考虑一下这个笨蛋
import {Component, OnInit, Input, OnChanges, DoCheck, ChangeDetectionStrategy, EventEmitter} from 'angular2/core'
@Component({
selector: 'child11',
template: `
<button (click)="change_obj()">Button in Child11</button>
<div>This is child11: {{my_obj11['name']}}</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class Child11 {
@Input()
my_obj11: Object;
change_obj(){
this.my_obj11['name'] = 'some other name';
}
}
@Component({
selector: 'child1',
template: `
<child11 [my_obj11]="my_obj1"></child11>
<div>This is child1: {{my_obj1['name']}}</div>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
directives: [Child11]
})
export class Child1 {
@Input()
my_obj1: Object;
}
@Component({
selector: 'parent',
template: `
<div>
<child1 [my_obj1]="my_obj" ></child1>
This is my_obj in parent: {{my_obj['name']}}
</div>
`,
directives: [Child1]
})
export class App {
my_obj: Object = {'name': 'name1'};
}
这里是组件之间的关系
Parent
|
Child1
|
Child11
我们注意到Child1
有changeDetection: ChangeDetectionStrategy.OnPush
上面的代码很简单,parent
发送一个对象child1
,发送完全相同的对象到child11
.
child11
然后更新对象的原语。
我们看到parent
andchild1
的对象都被更新了,即使child1
有changeDetection: ChangeDetectionStrategy.OnPush
我猜changeDetection: ChangeDetectionStrategy.OnPush
只有一种方式:从上到下。
真的吗?
如果是这样,这是有原因的吗?