角度-如何使用@Input字符串作为选择器?

时间:2019-07-10 09:55:57

标签: angular input selector viewchild

我想使用'@Input()'字符串作为选择器,但是我不能,选择器总是记录未定义的,这里是我的示例:

具体示例

/ ********* compA文件********* /

// compA -> html (call compB 3 times, every time keying in the different string in [expName])
<compB [expName]="'dog'">
  this is dog //ng-content
</compB>

<compB [expName]="'cat'">
  this is cat //ng-content
</compB>

<compB [expName]="'bird'">
  this is bird //ng-content
</compB>

// compA -> TS
... there is no need, I type string in the html

/ ********* compB文件(它是模板)********* /

// compB -> html
<div #{{expName}}>
    <ng-content></ng-content>
<div>

// compB -> TS
@Input() expName; // when compA using compB, he will type something string to [expName], and compB can read it from @Input.
...

@ViewChild('dog') dog;
@ViewChild('cat') cat;
@ViewChild('bird') bird;

在很多输入中,comA将狗,猫,鸟...输入到comB的[expName],所以comB使用@Input来获取它,我想,也许[expName]现在可以是#cat, html中的#dog,#bird。

ngOnInit(){
    console.log( this.dog );
}

现在我尝试登录#dog,但失败了

0 个答案:

没有答案