2017-04-12 4 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()時計を変更しますか?

test.a1変更を監視する方法
http.get(....).subscribe(res=>{ 
    test.a1=1; 
}) 

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

あなたは何を言っているのですか? –

関連する問題