angular2-changedetection

How to force a component's re-rendering in Angular 2?

时间秒杀一切 提交于 2019-11-26 11:05:53
How to force a component's re-rendering in Angular 2? For debug purposes working with Redux i'd like to force a component to re-render it's view, is that possible? Rendering happens after change detection. To force change detection, so that component property values that have changed get propagated to the DOM (and then the browser will render those changes in the view), here are some options: ApplicationRef.tick() - similar to Angular 1's $rootScope.$digest() -- i.e., check the full component tree NgZone.run(callback) - similar to $rootScope.$apply(callback) -- i.e., evaluate the callback

How to detect when an @Input() value changes in Angular?

二次信任 提交于 2019-11-26 07:50:59
问题 I have a parent component ( CategoryComponent ), a child component ( videoListComponent ) and an ApiService. I have most of this working fine i.e. each component can access the json api and get its relevant data via observables. Currently video list component just gets all videos, I would like to filter this to just videos in a particular category, I achieved this by passing the categoryId to the child via @Input() . CategoryComponent.html <video-list *ngIf=\"category\" [categoryId]=\

What is the Angular equivalent to an AngularJS $watch?

落爺英雄遲暮 提交于 2019-11-26 05:49:26
问题 In AngularJS you were able to specify watchers to observe changes in scope variables using the $watch function of the $scope . What is the equivalent of watching for variable changes (in, for example, component variables) in Angular? 回答1: In Angular 2, change detection is automatic... $scope.$watch() and $scope.$digest() R.I.P. Unfortunately, the Change Detection section of the dev guide is not written yet (there is a placeholder near the bottom of the Architecture Overview page, in section

What is the Angular equivalent to an AngularJS $watch?

眉间皱痕 提交于 2019-11-26 05:42:46
In AngularJS you were able to specify watchers to observe changes in scope variables using the $watch function of the $scope . What is the equivalent of watching for variable changes (in, for example, component variables) in Angular? Mark Rajcok In Angular 2, change detection is automatic... $scope.$watch() and $scope.$digest() R.I.P. Unfortunately, the Change Detection section of the dev guide is not written yet (there is a placeholder near the bottom of the Architecture Overview page, in section "The Other Stuff"). Here's my understanding of how change detection works: Zone.js "monkey

What&#39;s the difference between markForCheck() and detectChanges()

心已入冬 提交于 2019-11-26 02:40:39
问题 What is the difference between ChangeDetectorRef.markForCheck() and ChangeDetectorRef.detectChanges() ? I only found information on SO as to the difference between NgZone.run() , but not between these two functions. For answers with only a reference to the doc, please illustrate some practical scenarios to choose one over the other. 回答1: From docs : detectChanges() : void Checks the change detector and its children. It means, if there is a case where any thing inside your model (your class)

How to force a component&#39;s re-rendering in Angular 2?

一个人想着一个人 提交于 2019-11-26 02:17:40
问题 How to force a component\'s re-rendering in Angular 2? For debug purposes working with Redux i\'d like to force a component to re-render it\'s view, is that possible? 回答1: Rendering happens after change detection. To force change detection, so that component property values that have changed get propagated to the DOM (and then the browser will render those changes in the view), here are some options: ApplicationRef.tick() - similar to Angular 1's $rootScope.$digest() -- i.e., check the full

@ViewChild in *ngIf

戏子无情 提交于 2019-11-26 01:56:46
问题 Question What is the most elegant way to get @ViewChild after corresponding element in template was shown? Below is an example. Also Plunker available. Template: <div id=\"layout\" *ngIf=\"display\"> <div #contentPlaceholder></div> </div> Component: export class AppComponent { display = false; @ViewChild(\'contentPlaceholder\', {read: ViewContainerRef}) viewContainerRef; show() { this.display = true; console.log(this.viewContainerRef); // undefined setTimeout(()=> { console.log(this

ExpressionChangedAfterItHasBeenCheckedError Explained

夙愿已清 提交于 2019-11-26 00:36:26
问题 Please explain to me why I keep getting this error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Obviously, I only get it in dev mode, it doesn\'t happen on my production build, but it\'s very annoying and I simply don\'t understand the benefits of having an error in my dev environment that won\'t show up on prod --probably because of my lack of understanding. Usually, the fix is easy enough, I just wrap the error causing code in a setTimeout like

Triggering change detection manually in Angular

 ̄綄美尐妖づ 提交于 2019-11-25 22:56:34
问题 I\'m writing an Angular component that has a property Mode(): string . I would like to be able to set this property programmatically not in response to any event. The problem is that in the absence of a browser event, a template binding {{Mode}} doesn\'t update. Is there a way to trigger this change detection manually? 回答1: Try one of these: ApplicationRef.tick() - similar to AngularJS's $rootScope.$digest() -- i.e., check the full component tree NgZone.run(callback) - similar to $rootScope.

ExpressionChangedAfterItHasBeenCheckedError Explained

北城余情 提交于 2019-11-25 18:57:05
Please explain to me why I keep getting this error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Obviously, I only get it in dev mode, it doesn't happen on my production build, but it's very annoying and I simply don't understand the benefits of having an error in my dev environment that won't show up on prod --probably because of my lack of understanding. Usually, the fix is easy enough, I just wrap the error causing code in a setTimeout like this: setTimeout(()=> { this.isLoading = true; }, 0); Or force detect changes with a constructor like this: