Skip to content

Commit 0a83f2f

Browse files
committed
feat(directives): auto provide IONIC_DIRECTIVES to all components
Closes #6092
1 parent 17f9465 commit 0a83f2f

File tree

5 files changed

+23
-58
lines changed

5 files changed

+23
-58
lines changed

ionic/components/nav/test/basic/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import {Component, Type, ViewChild} from 'angular2/core';
22
import {App, NavController, Alert, Content} from 'ionic-angular';
33
import {Page, Config, IonicApp} from 'ionic-angular';
4-
import {NavParams, ViewController, IONIC_DIRECTIVES} from 'ionic-angular';;
4+
import {NavParams, ViewController} from 'ionic-angular';;
55

66

77
@Component({
88
selector: 'my-cmp',
9-
template: `<p>My Custom Component Test <ion-icon name="star"></ion-icon></p>`,
10-
directives: [IONIC_DIRECTIVES]
9+
template: `<p>My Custom Component Test <ion-icon name="star"></ion-icon></p>`
1110
})
1211
class MyCmpTest{}
1312

ionic/decorators/app.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, ChangeDetectionStrategy, ViewEncapsulation, enableProdMode, Type} from 'angular2/core';
1+
import {Component, ChangeDetectionStrategy, ViewEncapsulation, enableProdMode, Type, provide, PLATFORM_DIRECTIVES} from 'angular2/core';
22
import {bootstrap} from 'angular2/platform/browser';
33
import {ionicProviders, postBootstrap} from '../config/bootstrap';
44
import {IONIC_DIRECTIVES} from '../config/directives';
@@ -41,6 +41,9 @@ export interface AppMetadata {
4141
* property that has an inline template, or a `templateUrl` property that points
4242
* to an external html template. The `@App` decorator runs the Angular bootstrapping
4343
* process automatically, however you can bootstrap your app separately if you prefer.
44+
* Additionally, `@App` will automatically bootstrap with all of Ionic's
45+
* core components, meaning they won't all have to be individually imported and added
46+
* to each component's `directives` property.
4447
*
4548
* @usage
4649
* ```ts
@@ -71,9 +74,6 @@ export function App(args: AppMetadata = {}) {
7174

7275
args.selector = 'ion-app';
7376

74-
// auto add Ionic directives
75-
args.directives = args.directives ? args.directives.concat(IONIC_DIRECTIVES) : IONIC_DIRECTIVES;
76-
7777
// if no template was provided, default so it has a root <ion-nav>
7878
if (!args.templateUrl && !args.template) {
7979
args.template = '<ion-nav></ion-nav>';
@@ -88,6 +88,12 @@ export function App(args: AppMetadata = {}) {
8888
// define array of bootstrap providers
8989
let providers = ionicProviders(args).concat(args.providers || []);
9090

91+
// auto add Ionic directives
92+
let directives = args.directives ? args.directives.concat(IONIC_DIRECTIVES) : IONIC_DIRECTIVES;
93+
94+
// automatically provide all of Ionic's directives to every component
95+
providers.push(provide(PLATFORM_DIRECTIVES, {useValue: [directives], multi:true}));
96+
9197
if (args.prodMode) {
9298
enableProdMode();
9399
}

ionic/decorators/page.ts

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, ChangeDetectionStrategy, ViewEncapsulation, Type} from 'angular2/core';
2-
import {IONIC_DIRECTIVES} from '../config/directives';
32

43
const _reflect: any = Reflect;
54

@@ -33,12 +32,15 @@ export interface PageMetadata {
3332
* @description
3433
*
3534
* The Page decorator indicates that the decorated class is an Ionic
36-
* navigation component, meaning it can be navigated to using a NavController.
35+
* navigation component, meaning it can be navigated to using a
36+
* [NavController](../../nav/NavController).
3737
*
38-
* Pages have all `IONIC_DIRECTIVES`, which include all Ionic components and directives,
39-
* as well as Angular's [CORE_DIRECTIVES](https://angular.io/docs/js/latest/api/core/CORE_DIRECTIVES-const.html)
40-
* and [FORM_DIRECTIVES](https://angular.io/docs/js/latest/api/core/FORM_DIRECTIVES-const.html),
41-
* already provided to them, so you only need to supply custom components and directives to your pages:
38+
* Since the app has already been bootstrapped with Ionic's core directives, it
39+
* is not needed to include `IONIC_DIRECTIVES` in the directives property. Additionally,
40+
* Angular's [CORE_DIRECTIVES](https://angular.io/docs/ts/latest/api/common/CORE_DIRECTIVES-let.html)
41+
* and [FORM_DIRECTIVES](https://angular.io/docs/ts/latest/api/common/FORM_DIRECTIVES-let.html),
42+
* are also already provided, so you only need to supply any custom components and directives
43+
* to your pages:
4244
*
4345
* @usage
4446
*
@@ -53,44 +55,7 @@ export interface PageMetadata {
5355
* class MyPage {}
5456
* ```
5557
*
56-
* Here [Content](../../../components/content/Content/) will load because
57-
* it is in `IONIC_DIRECTIVES`, so there is no need to add a `directives` array.
58-
*
59-
*
60-
* Say you built a custom component that uses the already existing Ionic component.
61-
* In this case, you would add `IONIC_DIRECTIVES` to your directives array.
62-
*
63-
* ```ts
64-
* import {IONIC_DIRECTIVES} from 'ionic-angular';
65-
* @Component({
66-
* selector: 'my-component'
67-
* template: `<div class="my-style">
68-
* <ion-checkbox></ion-checkbox>
69-
* </div>`,
70-
* directives: [IONIC_DIRECTIVES]
71-
* })
72-
* class MyCustomCheckbox {}
73-
*```
74-
75-
* Alternatively, you could:
76-
*
77-
* ```ts
78-
* import {Checkbox, Icon} from 'ionic-angular'
79-
* ```
80-
*
81-
* along with any other components and add them individually:
82-
*
83-
* ```
84-
* @Component({
85-
* ...
86-
* directives: [Checkbox, Icon]
87-
* })
88-
* ```
89-
*
90-
* However, using IONIC_DIRECTIVES will always *Just Work* with no
91-
* performance overhead, so there is really no reason to not always use it.
92-
*
93-
* Pages have their content automatically wrapped in `<ion-view>`, so although
58+
* Pages have their content automatically wrapped in `<ion-page>`, so although
9459
* you may see these tags if you inspect your markup, you don't need to include
9560
* them in your templates.
9661
*
@@ -99,7 +64,6 @@ export interface PageMetadata {
9964
export function Page(config: PageMetadata) {
10065
return function(cls) {
10166
config.selector = 'ion-page';
102-
config.directives = config.directives ? config.directives.concat(IONIC_DIRECTIVES) : IONIC_DIRECTIVES;
10367
config.host = config.host || {};
10468
config.host['[hidden]'] = '_hidden';
10569
config.host['[class.tab-subpage]'] = '_tabSubPage';

tooling/generators/component/component.tmpl.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component} from 'angular2/core';
2-
import {IONIC_DIRECTIVES} from 'ionic-angular';
32

43
/*
54
Generated class for the <%= jsClassName %> component.
@@ -9,8 +8,7 @@ import {IONIC_DIRECTIVES} from 'ionic-angular';
98
*/
109
@Component({
1110
selector: '<%= fileName %>',
12-
templateUrl: 'build/<%= directory %>/<%= fileName %>/<%= fileName %>.html',
13-
directives: [IONIC_DIRECTIVES] // makes all Ionic directives available to your component
11+
templateUrl: 'build/<%= directory %>/<%= fileName %>/<%= fileName %>.html'
1412
})
1513
export class <%= jsClassName %> {
1614
constructor() {

tooling/generators/component/component.tmpl.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component} from 'angular2/core';
2-
import {IONIC_DIRECTIVES} from 'ionic-angular';
32

43
/*
54
Generated class for the <%= jsClassName %> component.
@@ -9,8 +8,7 @@ import {IONIC_DIRECTIVES} from 'ionic-angular';
98
*/
109
@Component({
1110
selector: '<%= fileName %>',
12-
templateUrl: 'build/<%= directory %>/<%= fileName %>/<%= fileName %>.html',
13-
directives: [IONIC_DIRECTIVES] // makes all Ionic directives available to your component
11+
templateUrl: 'build/<%= directory %>/<%= fileName %>/<%= fileName %>.html'
1412
})
1513
export class <%= jsClassName %> {
1614
constructor() {

0 commit comments

Comments
 (0)