typescript root 指令上的 Angular 2 输入参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33640178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:07:34  来源:igfitidea点击:

Angular 2 input parameters on root directive

angulartypescriptangular2-template

提问by redman

Thisexample 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:

这个例子展示了如何在子组件上使用 @Input() 注解。我的问题是你如何在根组件上使用它?例如,如果您修改上面链接上的代码:

@Component({
selector: 'app',
template: `
    <bank-account bank-name="RBC" account-id="4747"></bank-account>
`,
directives: [BankAccount]
})
class App {
    @Input() something: string;
}

bootstrap(App);

And in html:

在 html 中:

<app something="Test"></app>

The above example never updates somethingproperty on App component.

上面的例子从不更新App 组件上的某些属性。

回答by martin

I think you could still use:

我认为你仍然可以使用:

class App {
    constructor(elm: ElementRef) {
        this.something = elm.nativeElement.getAttribute('something'); 
    }
}

回答by alexpods

This Tobias Bosch's commenthas answer on your question:

Tobias Bosch 的评论回答了您的问题:

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.

这不起作用的原因是您放置 的 index.html<app something="Test"></app>不是角度组件。因此,Angular 不会编译这个元素。Angular 不会在运行时读取属性值,只在编译时读取,否则我们会受到性能影响。

So, at this moment you can't use input parameters on root element.

因此,此时您不能在根元素上使用输入参数。