一个非常基本的设置,组件:
export class LayerComponent {
public content: string;
constructor(content: string) {
this.content = content;
}
}
及其模板:
<p>{{content}}</p>
我想从另一个组件(静态地)实例化描述的传递内容参数的组件(不需要绑定(bind)它)。我采用了以下无效的方法:
<ipe-artboard-layer content="Joep"></ipe-artboard-layer>
<ipe-artboard-layer content="Floris"></ipe-artboard-layer>
<ipe-artboard-layer content="Casper"></ipe-artboard-layer>
这种方法是否可行,可取?我宁愿不进行真正的绑定(bind),因为它只是用组件的某些属性的一次性初始值来实例化组件。
最佳答案
据我所知,不可能以这种方式调用构造函数。
你要找的是 @Input()
绑定(bind):
<ipe-artboard-layer [content]="Joep"></ipe-artboard-layer>
在你的组件中:
export class LayerComponent {
@Input()
public content: string;
constructor() {}
}
在这里您可以阅读more about component interactions .
关于angular - 将参数从模板传递给构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48745421/