我的子組件ngOnChanges`工作如下:如何使``中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);
}
}
和家長組成如下:
'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'
};
}
}
每次當我點擊「更新」按鈕,ngOnChanges
方法永遠不會被調用,但爲什麼?
我我使用角版本是 「2.0.0-beta.8」
你確定嗎?因爲我認爲當你點擊'update'按鈕時會被解僱。 – micronyks