我发现在模板中使用模板引用变量存在一些问题。
当我在根级别声明相同的模板引用变量(如下所示)时,运行时编译器会抛出错误。
示例>
<input type="text" id="n-1" #myInput />
<input type="text" id="n-2" #myInput />
但是,当将重复的模板引用变量声明为子节点时,则没有问题。
示例>
<div>
<input type="text" id="n-1" #myInput />
<input type="text" id="n-2" #myInput />
</div>
这里是示例代码:https://embed.plnkr.co/kqTXfh/
我发现 Angular 编译器会检查模板引用变量声明的重复。
总而言之,我的问题是我可以在子节点中使用重复的模板引用变量吗?
这是预期的结果吗?
提前致谢。
最佳答案
回复很晚了。尽管如果有人来到此页面也有同样的疑问,请做出回应。
根据官方文档不要在同一模板中多次定义相同的变量名称,因为运行时值将是不可预测的。。
https://angular.io/guide/template-reference-variables#template-variable-scope
关于 Angular 2 : about duplicated declarations of Template Reference Variable in a template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41178810/