[英]Angular2 Create a component
我想在angular2中創建一個具有特定屬性的新組件,因此可以使用一個標簽
<my-cmp type="Type1"></my-cmp>
我嘗試了許多示例,但沒有一個起作用。 如果有人有任何可行的例子,請幫助我,謝謝。
謝謝哈立德
這個給你。 見這個笨蛋 。 用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);
}
}
最簡單的答案是@Component注釋,將任何打字稿類轉換為angular2組件。 任何標注為@Component({})的打字稿類都是angular2組件。 正如您在前面的答案中看到的那樣,兩個類都用@Component注釋。 組件將json對象作為參數來告訴angular2組件的行為。
@Component({
selector: 'my-app', //will be user in html as tag/attribut
template: ` //the html injection etc
<my-cmp type="Static Type"></my-cmp>
<my-cmp [type]="dynamicType + dynamicTypeIndex"></my-cmp>
`
})
export class AppCompoment { //we exported this class/component so that it can be imported
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.