2016-04-11 13 views
0

属性ディレクティブに問題があります。ディレクティブの文字列属性に関する問題

私は次のディレクティブに定義されている:

import {Directive, Input, ElementRef} from 'angular2/core'; 
@Directive({ 
    selector: '[chooseMe]' 
}) 

export class ChooseMe { 
    @Input('chooseMe') data: string; 
    constructor(private _elementRef:ElementRef) { 
    console.log(this.data);  
} 
} 

をそして、私はそのようにそれをフック:

<button [chooseMe]="example"> W/E</button> 

そしてもちろん、私のコンポーネントに私が持っている:しかし

@Component({ 
..., 
directives:[ChooseMe] 
}) 

this.dataはそれぞれ定義されていません。私のミスはどこですか?

答えて

1

ngOnChanges()が初めて呼び出される前に入力が設定されていません。
ngOnInit()が最初ngOnChanges()後に呼び出されます。

import {Directive, Input, ElementRef} from 'angular2/core'; 
@Directive({ 
    selector: '[chooseMe]' 
}) 
export class ChooseMe { 
    @Input('chooseMe') data: string; 
    constructor(private _elementRef:ElementRef) { 
    } 

    ngOnInit() { 
     console.log(this.data);  
    } 
} 
+1

ギュンターが正確である - ちょうどあなたのコードでそれをチェック - http://plnkr.co/edit/UoE1mIvfympVxPe6rNtd?p=preview – micronyks

+0

はたぶん 'example'は'まだあります定義されていない? –

+0

@GünterZöchbauer、あなたは正しいです - 私は普通の文字列を渡したいと思います。出来ますか? – uksz

関連する問題