thành phần con của tôi như sau:Làm thế nào để làm cho `tác ngOnChanges` trong` angular2`
'use strict';
import {Component, Input, OnInit, OnChanges, ChangeDetectionStrategy, ElementRef} from 'angular2/core';
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'my-app',
template: ''
})
export class MyApp implements OnInit {
@Input() options: any;
constructor(private el: ElementRef) {
}
ngOnInit() {
}
ngOnChanges(...args: any[]) {
console.log('changing', args);
}
}
và thành phần Chánh như sau đây:
'use strict';
import {Component, Input} from 'angular2/core';
import {MyApp} from './MyApp';
@Component({
selector: 'map-presentation',
template: `<my-app [options]="opts"></my-app>
<button (click)="updates($event)">UPDATES</button>
`,
directives: [MyApp]
})
export class MainApp {
opts: any;
constructor() {
this.opts = {
width: 500,
height: 600
};
}
updates() {
console.log('before changes');
this.opts = {
name: 'nanfeng'
};
}
}
Mỗi lần khi tôi nhấp vào nút "CẬP NHẬT" , phương pháp ngOnChanges
không bao giờ được gọi, nhưng tại sao?
tôi phiên bản góc cạnh tôi đang sử dụng là "2.0.0-beta.8"
bạn có chắc chắn không? bởi vì tôi nghĩ rằng nó bị sa thải khi bạn nhấp vào nút 'cập nhật '. – micronyks