gpt4 book ai didi

javascript - Angular2 创建组件

转载 作者:行者123 更新时间:2023-11-30 09:57:11 25 4
gpt4 key购买 nike

我想在 angular2 中创建一个具有特定属性的新组件,这样我就可以使用一个标签

<my-cmp type="Type1"></my-cmp>

我尝试了很多示例,但没有一个有效。如果有人有任何工作示例,请帮助我,谢谢。

谢谢哈立德

最佳答案

给你。参见 this plunker .用 TypeScript 编写:

import {Component, Input} from 'angular2/angular2'

@Component({
selector: 'my-cmp'
template: `
<div>
<b>Type:</b> {{ type }}
</div>
`
})
class MyComponent {
@Input() type;
}

@Component({
selector: 'my-app',
directives: [MyComponent],
template: `
<my-cmp type="Static Type"></my-cmp>
<my-cmp [type]="dynamicType + dynamicTypeIndex"></my-cmp>
`
})
export class App {
dynamicType: string = 'Dynamic Type ';
dynamicTypeIndex: number = 0;

constructor() {
setInterval(() => ++this.dynamicTypeIndex, 1000);
}
}

关于javascript - Angular2 创建组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33479481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com