2017-04-12 28 views
0
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> 

angular2 @Input()更改手錶?

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

如何觀看test.a1改變? 類似angular1 $觀看

回答

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> 
+0

無法自動比較?你想讓程序觸發事件嗎? – neo

+0

sorey你想說什麼? –