nav-controller.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. * @name NavController
  3. * @description
  4. *
  5. * NavController is the base class for navigation controller components like
  6. * [`Nav`](../../components/nav/Nav/) and [`Tab`](../../components/tabs/Tab/). You use navigation controllers
  7. * to navigate to [pages](#view-creation) in your app. At a basic level, a
  8. * navigation controller is an array of pages representing a particular history
  9. * (of a Tab for example). This array can be manipulated to navigate throughout
  10. * an app by pushing and popping pages or inserting and removing them at
  11. * arbitrary locations in history.
  12. *
  13. * The current page is the last one in the array, or the top of the stack if we
  14. * think of it that way. [Pushing](#push) a new page onto the top of the
  15. * navigation stack causes the new page to be animated in, while [popping](#pop)
  16. * the current page will navigate to the previous page in the stack.
  17. *
  18. * Unless you are using a directive like [NavPush](../../components/nav/NavPush/), or need a
  19. * specific NavController, most times you will inject and use a reference to the
  20. * nearest NavController to manipulate the navigation stack.
  21. *
  22. * ## Basic usage
  23. * The simplest way to navigate through an app is to create and initialize a new
  24. * nav controller using the `<ion-nav>` component. `ion-nav` extends the `NavController`
  25. * class.
  26. *
  27. * ```typescript
  28. * import { Component } from `@angular/core`;
  29. * import { StartPage } from './start-page';
  30. *
  31. * @Component(
  32. * template: `<ion-nav [root]="rootPage"></ion-nav>`
  33. * })
  34. * class MyApp {
  35. * // set the rootPage to the first page we want displayed
  36. * public rootPage: any = StartPage;
  37. *
  38. * constructor(){
  39. * }
  40. * }
  41. *
  42. * ```
  43. *
  44. * ### Injecting NavController
  45. * Injecting NavController will always get you an instance of the nearest
  46. * NavController, regardless of whether it is a Tab or a Nav.
  47. *
  48. * Behind the scenes, when Ionic instantiates a new NavController, it creates an
  49. * injector with NavController bound to that instance (usually either a Nav or
  50. * Tab) and adds the injector to its own providers. For more information on
  51. * providers and dependency injection, see [Dependency Injection](https://angular.io/docs/ts/latest/guide/dependency-injection.html).
  52. *
  53. * Instead, you can inject NavController and know that it is the correct
  54. * navigation controller for most situations (for more advanced situations, see
  55. * [Menu](../../menu/Menu/) and [Tab](../../tab/Tab/)).
  56. *
  57. * ```ts
  58. * import { NavController } from 'ionic-angular';
  59. *
  60. * class MyComponent {
  61. * constructor(public navCtrl: NavController) {
  62. *
  63. * }
  64. * }
  65. * ```
  66. *
  67. * ### Navigating from the Root component
  68. * What if you want to control navigation from your root app component?
  69. * You can't inject `NavController` because any components that are navigation
  70. * controllers are _children_ of the root component so they aren't available
  71. * to be injected.
  72. *
  73. * By adding a reference variable to the `ion-nav`, you can use `@ViewChild` to
  74. * get an instance of the `Nav` component, which is a navigation controller
  75. * (it extends `NavController`):
  76. *
  77. * ```typescript
  78. *
  79. * import { Component, ViewChild } from '@angular/core';
  80. * import { NavController } from 'ionic-angular';
  81. *
  82. * @Component({
  83. * template: '<ion-nav #myNav [root]="rootPage"></ion-nav>'
  84. * })
  85. * export class MyApp {
  86. * @ViewChild('myNav') nav: NavController
  87. * public rootPage: any = TabsPage;
  88. *
  89. * // Wait for the components in MyApp's template to be initialized
  90. * // In this case, we are waiting for the Nav with reference variable of "#myNav"
  91. * ngOnInit() {
  92. * // Let's navigate from TabsPage to Page1
  93. * this.nav.push(Page1);
  94. * }
  95. * }
  96. * ```
  97. *
  98. * ### Navigating from an Overlay Component
  99. * What if you wanted to navigate from an overlay component (popover, modal, alert, etc)?
  100. * In this example, we've displayed a popover in our app. From the popover, we'll get a
  101. * reference of the root `NavController` in our app, using the `getRootNav()` method.
  102. *
  103. *
  104. * ```typescript
  105. * import { Component } from '@angular/core';
  106. * import { App, ViewController } from 'ionic-angular';
  107. *
  108. * @Component({
  109. * template: `
  110. * <ion-content>
  111. * <h1>My PopoverPage</h1>
  112. * <button ion-button (click)="pushPage()">Call pushPage</button>
  113. * </ion-content>
  114. * `
  115. * })
  116. * class PopoverPage {
  117. * constructor(
  118. * public viewCtrl: ViewController
  119. * public appCtrl: App
  120. * ) {}
  121. *
  122. * pushPage() {
  123. * this.viewCtrl.dismiss();
  124. * this.appCtrl.getRootNav().push(SecondPage);
  125. * }
  126. * }
  127. *```
  128. *
  129. *
  130. * ## View creation
  131. * Views are created when they are added to the navigation stack. For methods
  132. * like [push()](#push), the NavController takes any component class that is
  133. * decorated with `@Component` as its first argument. The NavController then
  134. * compiles that component, adds it to the app and animates it into view.
  135. *
  136. * By default, pages are cached and left in the DOM if they are navigated away
  137. * from but still in the navigation stack (the exiting page on a `push()` for
  138. * example). They are destroyed when removed from the navigation stack (on
  139. * [pop()](#pop) or [setRoot()](#setRoot)).
  140. *
  141. * ## Pushing a View
  142. * To push a new view onto the navigation stack, use the `push` method.
  143. * If the page has an [`<ion-navbar>`](../../navbar/Navbar/),
  144. * a back button will automatically be added to the pushed view.
  145. *
  146. * Data can also be passed to a view by passing an object to the `push` method.
  147. * The pushed view can then receive the data by accessing it via the `NavParams`
  148. * class.
  149. *
  150. * ```typescript
  151. * import { Component } from '@angular/core';
  152. * import { NavController } from 'ionic-angular';
  153. * import { OtherPage } from './other-page';
  154. * @Component({
  155. * template: `
  156. * <ion-header>
  157. * <ion-navbar>
  158. * <ion-title>Login</ion-title>
  159. * </ion-navbar>
  160. * </ion-header>
  161. *
  162. * <ion-content>
  163. * <button ion-button (click)="pushPage()">
  164. * Go to OtherPage
  165. * </button>
  166. * </ion-content>
  167. * `
  168. * })
  169. * export class StartPage {
  170. * constructor(public navCtrl: NavController) {
  171. * }
  172. *
  173. * pushPage(){
  174. * // push another page onto the navigation stack
  175. * // causing the nav controller to transition to the new page
  176. * // optional data can also be passed to the pushed page.
  177. * this.navCtrl.push(OtherPage, {
  178. * id: "123",
  179. * name: "Carl"
  180. * });
  181. * }
  182. * }
  183. *
  184. * import { NavParams } from 'ionic-angular';
  185. *
  186. * @Component({
  187. * template: `
  188. * <ion-header>
  189. * <ion-navbar>
  190. * <ion-title>Other Page</ion-title>
  191. * </ion-navbar>
  192. * </ion-header>
  193. * <ion-content>I'm the other page!</ion-content>`
  194. * })
  195. * class OtherPage {
  196. * constructor(private navParams: NavParams) {
  197. * let id = navParams.get('id');
  198. * let name = navParams.get('name');
  199. * }
  200. * }
  201. * ```
  202. *
  203. * ## Removing a view
  204. * To remove a view from the stack, use the `pop` method.
  205. * Popping a view will transition to the previous view.
  206. *
  207. * ```ts
  208. * import { Component } from '@angular/core';
  209. * import { NavController } from 'ionic-angular';
  210. *
  211. * @Component({
  212. * template: `
  213. * <ion-header>
  214. * <ion-navbar>
  215. * <ion-title>Other Page</ion-title>
  216. * </ion-navbar>
  217. * </ion-header>
  218. * <ion-content>I'm the other page!</ion-content>`
  219. * })
  220. * class OtherPage {
  221. * constructor(public navCtrl: NavController ){
  222. * }
  223. *
  224. * popView(){
  225. * this.navCtrl.pop();
  226. * }
  227. * }
  228. * ```
  229. *
  230. * ## Lifecycle events
  231. * Lifecycle events are fired during various stages of navigation. They can be
  232. * defined in any component type which is pushed/popped from a `NavController`.
  233. *
  234. * ```ts
  235. * import { Component } from '@angular/core';
  236. *
  237. * @Component({
  238. * template: 'Hello World'
  239. * })
  240. * class HelloWorld {
  241. * ionViewDidLoad() {
  242. * console.log("I'm alive!");
  243. * }
  244. * ionViewWillLeave() {
  245. * console.log("Looks like I'm about to leave :(");
  246. * }
  247. * }
  248. * ```
  249. *
  250. * | Page Event | Returns | Description |
  251. * |---------------------|-----------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  252. * | `ionViewDidLoad` | void | Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The `ionViewDidLoad` event is good place to put your setup code for the page. |
  253. * | `ionViewWillEnter` | void | Runs when the page is about to enter and become the active page. |
  254. * | `ionViewDidEnter` | void | Runs when the page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page. |
  255. * | `ionViewWillLeave` | void | Runs when the page is about to leave and no longer be the active page. |
  256. * | `ionViewDidLeave` | void | Runs when the page has finished leaving and is no longer the active page. |
  257. * | `ionViewWillUnload` | void | Runs when the page is about to be destroyed and have its elements removed. |
  258. * | `ionViewCanEnter` | boolean/Promise&lt;void&gt; | Runs before the view can enter. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can enter |
  259. * | `ionViewCanLeave` | boolean/Promise&lt;void&gt; | Runs before the view can leave. This can be used as a sort of "guard" in authenticated views where you need to check permissions before the view can leave |
  260. *
  261. *
  262. * ## Nav Guards
  263. *
  264. * In some cases, a developer should be able to control views leaving and entering. To allow for this, NavController has the `ionViewCanEnter` and `ionViewCanLeave` methods.
  265. * Similar to Angular route guards, but are more integrated with NavController. For example, if you wanted to prevent a user from leaving a view:
  266. *
  267. * ```ts
  268. * export class MyClass{
  269. * constructor(
  270. * public navCtrl: NavController
  271. * ){}
  272. *
  273. * pushPage(){
  274. * this.navCtrl.push(DetailPage);
  275. * }
  276. *
  277. * ionViewCanLeave(): boolean{
  278. * // here we can either return true or false
  279. * // depending on if we want to leave this view
  280. * if(isValid(randomValue)){
  281. * return true;
  282. * } else {
  283. * return false;
  284. * }
  285. * }
  286. * }
  287. * ```
  288. *
  289. * We need to make sure that our `navCtrl.push` has a catch in order to catch the and handle the error.
  290. * If you need to prevent a view from entering, you can do the same thing
  291. *
  292. * ```ts
  293. * export class MyClass{
  294. * constructor(
  295. * public navCtrl: NavController
  296. * ){}
  297. *
  298. * pushPage(){
  299. * this.navCtrl.push(DetailPage);
  300. * }
  301. *
  302. * }
  303. *
  304. * export class DetailPage(){
  305. * constructor(
  306. * public navCtrl: NavController
  307. * ){}
  308. * ionViewCanEnter(): boolean{
  309. * // here we can either return true or false
  310. * // depending on if we want to enter this view
  311. * if(isValid(randomValue)){
  312. * return true;
  313. * } else {
  314. * return false;
  315. * }
  316. * }
  317. * }
  318. * ```
  319. *
  320. * Similar to `ionViewCanLeave` we still need a catch on the original `navCtrl.push` in order to handle it properly.
  321. * When handling the back button in the `ion-navbar`, the catch is already taken care of for you by the framework.
  322. *
  323. * ## NavOptions
  324. *
  325. * Some methods on `NavController` allow for customizing the current transition.
  326. * To do this, we can pass an object with the modified properites.
  327. *
  328. *
  329. * | Property | Value | Description |
  330. * |-----------|-----------|------------------------------------------------------------------------------------------------------------|
  331. * | animate | `boolean` | Whether or not the transition should animate. |
  332. * | animation | `string` | What kind of animation should be used. |
  333. * | direction | `string` | The conceptual direction the user is navigating. For example, is the user navigating `forward`, or `back`? |
  334. * | duration | `number` | The length in milliseconds the animation should take. |
  335. * | easing | `string` | The easing for the animation. |
  336. *
  337. * The property 'animation' understands the following values: `md-transition`, `ios-transition` and `wp-transition`.
  338. *
  339. * @see {@link /docs/components#navigation Navigation Component Docs}
  340. */
  341. var NavController = (function () {
  342. function NavController() {
  343. }
  344. return NavController;
  345. }());
  346. export { NavController };
  347. //# sourceMappingURL=nav-controller.js.map