angular2 @Input()更改手表?

时间:2017-04-12 10:23:11

标签: angular

export class Test {
  a1: number;
  name: string;
}


@Component({
  selector: '[t1-info]',
  templateUrl: './t1component.html',
})
export class T1InfoComponent implements OnInit {
  @Input() test: Test;
  constructor() { }

  ngOnInit() {

  }

}

<div t1-info [test]="test"></div>
<button (click)='test.a1 = test.a1-1'>test</button>

http.get(....).subscribe(res=>{
   test.a1=1;
})

如何观看test.a1更改? 与angular1 $ Watch

类似

1 个答案:

答案 0 :(得分:1)

您可以使用@output()触发该组件中所做的任何更改,并调用任何函数来侦听更改

export class Test {
  a1: number;
  name: string;
}


@Component({
  selector: '[t1-info]',
  templateUrl: './t1component.html',
})
export class T1InfoComponent implements OnInit {
  @Input() test: Test;
  @Output() changeEvent: new EventEmitter();
  constructor() { }

  ngOnInit() {

  }

  changeEvent(){
    console.log(this.test, "Changes")
  }
}

<div t1-info [test]="test"></div>
<button (click)='test.a1 = test.a1-1'>test</button>