2016-04-25 1 views
0

ディレクティブをグローバルにする方法はありますか?私は角度2で完全に制御されていないページのどこからでも追加する必要があります。 角度2で作成した日付ピッカー、グリッド、画像リストなどを作成して、 HTMLのこの種に似た他のframwork作成者:angular2datetimepickerは角2で作成したコンポーネントまたはカスタムディレクティブですが、角度2範囲外で使用グローバルなディレクティブを作るにはどうすればよいですか?角度2で完全に制御されていないページのどこでも使用できます

<body> 
    <label>birth of date</label> 
    <input type= "text" id='dob' angular2datetimepicker > 
    <label>Join Date</label> 
    <input type= "text" id= 'jod' angular2datetimepicker > 
</body> 

あなたは bootstrapそれが PLATFORM_DIRECTIVESを使用でき

答えて

3

アプリケーションのすべてのコンポーネントで利用可能なディレクティブの配列を作るために、アプリケーションをブートストラップするときに提供することができ、トークンを。以下の例を実行する

import {PLATFORM_DIRECTIVES} from 'angular2/core'; 
import {OtherDirective} from './myDirectives'; 
@Component({ 
    selector: 'my-component', 
    template: ` 
    <!-- can use other directive even though the component does not list it in `directives` --> 
    <other-directive></other-directive> 
    ` 
}) 
export class MyComponent { 
    ... 
} 
bootstrap(MyComponent, [provide(PLATFORM_DIRECTIVES, {useValue: [OtherDirective], multi:true})]); 

import {bootstrap} from 'angular2/platform/browser'; 
import {PLATFORM_DIRECTIVES, provide} from 'angular2/core'; 
import {HighlightDirective} from './highlight.directive'; 
import {AppComponent} from './app.component'; 

bootstrap(AppComponent, [ 
     provide(PLATFORM_DIRECTIVES, {useValue: HighlightDirective, multi: true}) 
]); 

demo plunker hereを参照してください。

+0

@acdcjuniorに感謝します。私の状況では、主にこのページを制御する他のフレームワークがあり、componenent.htmlに移動できない他の要素が含まれているため、この指示はメインページindex.htmlに追加する必要があります。 – Nahed

+0

他のフレームワークは何ですか?これらの要件がある場合は、 'window'オブジェクトに制御するオブジェクトを追加し、' bootstrap() 'を実行するときにそのオブジェクトを参照することができます。 – acdcjunior

+0

ありがとうございます、警告が表示されます警告:安全でないスタイルの値function(){ return(this.layout === 'column')をサニタイズしますか? '列行'; }(http://g.co/ng/security#xssを参照)。 Angular 2.0.0のアップデートはありますか? –

関連する問題