UNPKG

14.5 kBJavaScriptView Raw
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>`](../../components/toolbar/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 * Those events are only fired on IonicPage, for classic Angular Component, use [Angular Lifecycle Hooks](https://angular.io/guide/lifecycle-hooks).
262 *
263 * ## Nav Guards
264 *
265 * 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.
266 * Similar to Angular route guards, but are more integrated with NavController. For example, if you wanted to prevent a user from leaving a view:
267 *
268 * ```ts
269 * export class MyClass{
270 * constructor(
271 * public navCtrl: NavController
272 * ){}
273 *
274 * pushPage(){
275 * this.navCtrl.push(DetailPage);
276 * }
277 *
278 * ionViewCanLeave(): boolean{
279 * // here we can either return true or false
280 * // depending on if we want to leave this view
281 * if(isValid(randomValue)){
282 * return true;
283 * } else {
284 * return false;
285 * }
286 * }
287 * }
288 * ```
289 *
290 * We need to make sure that our `navCtrl.push` has a catch in order to catch the and handle the error.
291 * If you need to prevent a view from entering, you can do the same thing
292 *
293 * ```ts
294 * export class MyClass{
295 * constructor(
296 * public navCtrl: NavController
297 * ){}
298 *
299 * pushPage(){
300 * this.navCtrl.push(DetailPage);
301 * }
302 *
303 * }
304 *
305 * export class DetailPage(){
306 * constructor(
307 * public navCtrl: NavController
308 * ){}
309 * ionViewCanEnter(): boolean{
310 * // here we can either return true or false
311 * // depending on if we want to enter this view
312 * if(isValid(randomValue)){
313 * return true;
314 * } else {
315 * return false;
316 * }
317 * }
318 * }
319 * ```
320 *
321 * Similar to `ionViewCanLeave` we still need a catch on the original `navCtrl.push` in order to handle it properly.
322 * When handling the back button in the `ion-navbar`, the catch is already taken care of for you by the framework.
323 *
324 * ## NavOptions
325 *
326 * Some methods on `NavController` allow for customizing the current transition.
327 * To do this, we can pass an object with the modified properites.
328 *
329 *
330 * | Property | Value | Description |
331 * |-----------|-----------|------------------------------------------------------------------------------------------------------------|
332 * | animate | `boolean` | Whether or not the transition should animate. |
333 * | animation | `string` | What kind of animation should be used. |
334 * | direction | `string` | The conceptual direction the user is navigating. For example, is the user navigating `forward`, or `back`? |
335 * | duration | `number` | The length in milliseconds the animation should take. |
336 * | easing | `string` | The easing for the animation. |
337 *
338 * The property 'animation' understands the following values: `md-transition`, `ios-transition` and `wp-transition`.
339 *
340 * @see {@link /docs/components#navigation Navigation Component Docs}
341 */
342var NavController = (function () {
343 function NavController() {
344 }
345 return NavController;
346}());
347export { NavController };
348//# sourceMappingURL=nav-controller.js.map
\No newline at end of file