【发布时间】:2016-09-07 21:08:45
【问题描述】:
如何实现?
我的子组件
import {Component,Input,ngOnInit} from 'angular2/core';
@Component({
selector: 'my-component',
template: `
<div>In child component - myAttr = {{ myAttr1 }}</div>
`
})
export class MyComponent {
@Input() myAttr: number;
myAttr1:number;
ngOnInit()
{
this.myAttr1=this.myAttr*10;
}
}
主要组件
import {Component} from 'angular2/core';
import {MyComponent} from './sub.component';
@Component({
selector: 'my-app',
template: `
<my-component
[(myAttr)]="myAttr"
></my-component>
<button (click)="onClick()">Click</button>
`,
directives: [MyComponent]
})
export class AppComponent {
myAttr: number = 1;
onClick() {
console.log('Click in the parent component');
this.myAttr += 1;
}
}
我需要更新每次点击的值。应该有直接的方法,否则 Angular2 团队应该考虑这个
【问题讨论】:
标签: properties angular components