2017-11-08 5 views
0

イオンラベル要素に文字列を追加する指令があります。イオンラベルのテキストの変更方法

/** 
* icon used everywhere to indicate this is a required form field 
* usage: <ion-label required> blah </ion-label> 
* outputs: <ion-label required> blah <span>*</span> </ion-label> 
*/ 
@Directive({ 
    selector: 'ion-label[required]', 
}) 
export class ReqIconComponent { 

    constructor(el: ElementRef) { 
    debugger; 
    el.nativeElement.innerHTML = el.nativeElement.innerHTML.concat('<span> * </span>'); 
    } 

} 

イオンラベル要素内の "blah"テキスト値にアクセスするにはどうすればよいですか?

innerHTMLとinnerTextは両方とも ""です。 子ノードと子は両方とも長さが0です。

+0

上記の結果をされては、<イオンラベル...> *何とか – Stevko

+1

ngAfterViewInitに – Duannx

+1

おかげで、あなたのコードを移動してください。答えを作成し、私はそれを受け入れるでしょう。 – Stevko

答えて

1

コンストラクタでは、ビューがレンダリングされないため、innerHTMLを取得できません。コードをngAfterViewInitに移動して、表示が完全にレンダリングされるのを待ちます。作品 -

ngAfterViewInit(){ 
    this.el.nativeElement.innerHTML = this.el.nativeElement.innerHTML.concat('<span> * </span>'); 
} 
関連する問題