2016-08-23 7 views
3

角度2ディレクティブで定義されたメソッドを呼び出すためにどのように、私が注入されているディレクティブは私は2角度に新しいです

@Component({ 
    selector: 'select-me', 
    templateUrl: 'app/template.html', 
    directives: [BackgroundChange] // I want to access this same instance to make the dynamic changes 
}) 

export class PageComponent { 
constructor(private backgroundChange:BackgroundChange) { 
// I guess this will create new instance of BackgroundChange 
} 

} 

私はPageComponentから動的にいくつかの変更を行うにはBackgroundChangeで空くのメソッドを呼び出したいです

答えて

1

ディレクティブはスタンドアロンであり、HTMLテンプレートで使用する必要があります。あなたのPageComponentに挿入されていない... akaディレクティブは注射できません。

BackgroundChangeディレクティブは、テンプレートに添付されているHTML要素にアクセスできるため、内部で操作する必要があります。

角度ドキュメント:https://angular.io/docs/ts/latest/guide/attribute-directives.html#!#apply-directive

関連する問題