我有一个场景,我希望在我的组件中同时使用ngOnChanges和ngDoCheck,但是我记得前段时间在 angular 文档中一次只能使用其中一个。
虽然我似乎再也找不到这些信息,但我认为它之前在本节的某个地方说过。
使用这两者是否安全,还是我需要实现我自己的ngOnChangesin版本DoCheck以避免大“不”?
我正在阅读有关 Zone.JS 和 Angular 更改检测过程的信息,在代码源中,Zone.JS 通过将检查传播到所有组件来通知 Angular 有关更改并在第一个组件中从上到下运行验证,但在我的测试,我有奇怪的行为。
检查我的样本:
http://plnkr.co/edit/QPHBQ7cYg9JFZr2oVnGZ?p=preview
该示例具有以下架构:
<app-component>
<child-component>
<grand-child-component>
Run Code Online (Sandbox Code Playgroud)
它们相互嵌套,都没有 @Input 属性,而且都是 OnPush,它们在视图中显示一个简单的值,如:
看法:
{{getComponentValue}}
打字稿
value = "app component";
get getComponentValue() { // <-- ES6 get to log when Angular check
console.log('change detection checking here in component app-component')
return this.value;
}
Run Code Online (Sandbox Code Playgroud)
您可以在示例中检查此代码。
当应用程序启动时,我们可以看到日志:
check component app
check component child
check component grand child
Run Code Online (Sandbox Code Playgroud)
很酷,但是,现在让我们创建一个场景,如果孙子触发 DOM 事件?
改变孙子的观点
<button (click)="undefined"> {{getComponentValue}} </button>
<!-- (click)="undefined" trigger an event -->
Run Code Online (Sandbox Code Playgroud)
然后我们有日志:
check component app
check component child …Run Code Online (Sandbox Code Playgroud) 这可能是我自己的2变化检测如何角的作品,但我本来期望,如果一个组件的误解ChangeDetectionStrategy设置为Checked,CheckOnce或Detached,那该组件是只检查时组件实例一次变化。它似乎不会那样发生。
import {Component, OnInit, ChangeDetectionStrategy} from 'angular2/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<button
(click)="onClick($event)"
[class.thing]="hasThingClass()">Update</button>
</div>
`,
changeDetection:ChangeDetectionStrategy.CheckOnce,
styles: [`
.thing { background-color: red }
`]
})
export class App implements OnInit {
public hasThing:Boolean = false;
onClick() {
this.hasThing = !this.hasThing;
}
hasThingClass(e) {
return this.hasThing;
}
}
Run Code Online (Sandbox Code Playgroud)
单击时,我本来希望切换hasThing属性,但我不希望视图更新。碰巧,视图会更新。ChangeDetectionStrategy设置为Detached也时发生。
http://plnkr.co/edit/2hHdt2BDpj419Z32iPjJ?p=preview
我在这里缺少什么?究竟是什么导致视图更新?从我所见,无论我更新hasThing属性,无论值是否已更改,视图都会在单击时更新。
我有一个父函数ngOnInit(),它从谷歌地图获取值,如下所示
instance.input = document.getElementById('google_places_ac');
autocomplete = new google.maps.places.Autocomplete(instance.input, { types: ['(cities)']});
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
instance.setValue(place.address_components[3].long_name, place.address_components[2].long_name, place.address_components[1].long_name);
});
Run Code Online (Sandbox Code Playgroud)
setValue()是与共享服务共享价值的函数,在 html 页面上我有与父级和子级相同的内容
<input id="google_places_ac" [(attr.state)]="state" [(attr.country)]="coutnry" name="google_places_ac" type="text" value="{{city}}" class="form-control" />
在父组件类中,我对setValue()函数触发更改检测
setValue(a, b, c) {
this.coutnry = a;
this.state = b;
this.city = c;
this.sharedService.country = this.coutnry;
this.sharedService.city = this.city;
this.sharedService.state = this.state;
this.cdr.detectChanges();
// console.log(this.coutnry, this.state, this.city);
}
Run Code Online (Sandbox Code Playgroud)
这在父级上运行良好,但在子级上没有发生更改,我创建了一个单击函数,该函数在子级上触发更改检测,这也有效,但我希望它从父级自动触发,有什么解决方法吗?
我有一个组件,subscribes到Observable从service.当observable发出其next值时,我的组件中会发生一些变化.奇怪的是,组件将根据next值的生成方式更新或不更新视图.如果我理解这一点,一些事件已经被角度2修补,以触发变化检测,而有些事件则没有.MIDI事件是那些没有(有点感觉)的事件之一.
如果服务查找单击事件以触发下一个值,则更改将反映在视图中.因此发生了变化检测.
如果服务查找键盘事件相同的事情,则发生更改检测.
但是,当服务使用MIDI事件时,不会发生更改检测.
这里有一些代码:
export class App {
arr = [
{id: 1, status: false},
{id: 2, status: false},
{id: 3, status: false}
];
thing = new Subject<boolean>();
constructor(private renderer: Renderer,
private ser:Ser,
private midi: MidiService) {}
ngOnInit(){
// listening to click events, will trigger change detection
this.renderer.listenGlobal('document', 'click', (event) => {
console.log("Left click", event);
this.thing.next(1);
});
this.thing.subscribe( v => this.toggle(v));
// my midi service events will …Run Code Online (Sandbox Code Playgroud)