Angular 4 ExpressionChangedAfterItHasBeenCheckedError

The article Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError error explains this behavior in great details Cause Your problem is very similar to this one but instead of updating parent property through a service you’re updating it through synchronous event broadcasting. Here is the quote from the linked answer: During digest cycle Angular performs … Read more

Creating an angular2 component with ng-content dynamically

There is the projectableNodes parameter for the vcRef.createComponent method createComponent<C>(componentFactory: ComponentFactory<C>, index?: number, injector?: Injector, projectableNodes?: any[][], ngModule?: NgModuleRef<any>): ComponentRef<C>; You can use it to dynamically inject one component into another. Let’s say we have the following component @Component({ selector: ‘card’, template: ` <div class=”card__top”> <h2>Creating a angular2 component with ng-content dynamically</h2> </div> <div class=”card__body”> … Read more

How to Update a Component without refreshing full page – Angular

You can use a BehaviorSubject for communicating between different components throughout the app. You can define a data sharing service containing the BehaviorSubject to which you can subscribe and emit changes. Define a data sharing service import { Injectable } from ‘@angular/core’; import { BehaviorSubject } from ‘rxjs’; @Injectable() export class DataSharingService { public isUserLoggedIn: … Read more

Call child component method from parent class – Angular

You can do this by using @ViewChild for more info check this link With type selector child component @Component({ selector: ‘child-cmp’, template: ‘<p>child</p>’ }) class ChildCmp { doSomething() {} } parent component @Component({ selector: ‘some-cmp’, template: ‘<child-cmp></child-cmp>’, directives: [ChildCmp] }) class SomeCmp { @ViewChild(ChildCmp) child:ChildCmp; ngAfterViewInit() { // child is set this.child.doSomething(); } } With … Read more

What is the equivalent of ngShow and ngHide in Angular 2+?

The hidden property can be used for that [hidden]=”!myVar” See also https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/hidden issues hidden has some issues though because it can conflict with CSS for the display property. See how some in Plunker example doesn’t get hidden because it has a style :host {display: block;} set. (This might behave differently in other browsers – I … Read more

How can I select an element in a component template?

Instead of injecting ElementRef and using querySelector or similar from there, a declarative way can be used instead to access elements in the view directly: <input #myname> @ViewChild(‘myname’) input; element ngAfterViewInit() { console.log(this.input.nativeElement.value); } StackBlitz example @ViewChild() supports directive or component type as parameter, or the name (string) of a template variable. @ViewChildren() also supports … Read more