我的孩子组件如下:
'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);
}
}
Run Code Online (Sandbox Code Playgroud)
和Parent组件如下:
'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'
};
}
}
Run Code Online (Sandbox Code Playgroud)
每当我点击"更新"按钮时,该ngOnChanges方法永远不会被调用,但为什么呢?
我正在使用的角度版本是"2.0.0-beta.8"
mic*_*yks 15
这是工作
app.ts
import {Component} from 'angular2/core';
import {child} from 'src/child';
@Component({
selector: 'my-app',
providers: [],
template: `
<child-cmp [options]="opts"></child-cmp>
<button (click)="updates($event)">UPDATES</button>
`,
directives: [child]
})
export class App {
opts: any;
constructor() {
this.opts = {
width: 500,
height: 600
};
}
updates() {
console.log('after changes');
this.opts = {
name: 'micronyks'
};
}
};
Run Code Online (Sandbox Code Playgroud)
child.ts
import {Input,Component,Output,EventEmitter} from 'angular2/core';
import {Component, Input, OnInit, OnChanges, ChangeDetectionStrategy, ElementRef} from 'angular2/core';
@Component({
selector: 'child-cmp',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
`
})
export class child {
@Input() options: any;
ngOnChanges(...args: any[]) {
console.log('onChange fired');
console.log('changing', args);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68391 次 |
| 最近记录: |