0

我正在尝试根据输入装饰器更改圆环图的值。我能够初始化该值,但无法进一步更改它。

我正在使用<input type="number" [(ngModel)]="complete">2 路数据绑定值。但它不起作用。我认为它不起作用,因为模板已经被调用,我们稍后会更改数据。

有什么解决办法吗?

工作代码:http ://plnkr.co/edit/hYlFp1BX8ebixQMqAtNj?p=preview

父组件代码:

@Component({
  selector: 'my-app',
  providers: [],
  template: `


    <test-component [complete]="complete"></test-component>
    Completed %:<input type="number" [(ngModel)]="complete">

  `,
  directives: [TestComponent]
})
export class App {
  complete:number=40;
  constructor(){

  }
  test(){
    this.complete=60;
  }
}
4

1 回答 1

0

at 指令正在接收complete父组件的值更改。@Input() complete

您的图表是未更新的图表。每次值更改时,您都必须重新绘制整个图表。

complete我的建议:Observable<integer>每次用户更改complete <input>.

相关变化:

@Component({
    ...
    <test-component [complete]="complete"></test-component>
    Completed %:<input type="number" [(ngModel)]="complete">
...
export class App {
  complete:number=40;
  constructor(){

变成:

@Component({
    ...
    <test-component [complete]="completeObs"></test-component>
    Completed %:<input type="number" [(ngModel)]="complete" 
                                               (ngModelChange)="completeObs.next($event)">
...
export class App {
  complete:number=40;
  completeObs:Observable<integer> = new BehaviorSubject<integer>(this.complete);
  constructor(){

您还需要更改指令:

export class TestComponent{
  @Input() complete:Observable<integer>;

  ngAfterViewInit() {
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    var colors=['green','orange'];
    var labels=['Completed','Pending'];

    this.complete.subscribe((complete) => {      // <-- notice it subscribes to the input
      let incomplete:integer = 100 - complete;

在此处查看 plunker 演示

于 2016-08-05T00:08:49.387 回答