root 指令上的 Angular 2 输入参数 [英] Angular 2 input parameters on root directive
问题描述
这个示例展示了如何在子节点上使用@Input()注解成分.我的问题是你如何在根组件上使用它?例如,如果您修改上面链接上的代码:
This example shows how to use @Input() annotation on child components. My question is how do you use it on root component? For example if you modify code on the link above:
@Component({
selector: 'app',
template: `
<bank-account bank-name="RBC" account-id="4747"></bank-account>
`,
directives: [BankAccount]
})
class App {
@Input() something: string;
}
bootstrap(App);
在 html 中:
<app something="Test"></app>
上面的例子从不更新 App 组件的 something 属性.
The above example never updates something property on App component.
推荐答案
Tobias博世的评论已回答您的问题:
这不起作用的原因是您放置 <app something="Test"></app>
的 index.html 不是一个角度组件.因此,Angular 不会编译这个元素.Angular 不会在运行时读取属性值,只会在编译时读取,否则我们会受到性能影响.
The reason why this is not working is that your index.html in which you place the
<app something="Test"></app>
is not an angular component. Because of this, Angular won't compile this element. And Angular does not read attribute values during runtime, only during compile time, as otherwise we would get a performance hit.
因此,此时您不能在根元素上使用输入参数.
So, at this moment you can't use input parameters on root element.
这篇关于root 指令上的 Angular 2 输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!