Skip to content

Commit

Permalink
chore(): update documentation with the way to use media.broadcast wit…
Browse files Browse the repository at this point in the history
…h layouts in ngAfterViewInit (#685)

angular is more restrictive now and complains if we dont excplicitly call a change detection cycle when changing any input via a life hook cycle such as ngAfterViewInit and ngAfterContentInit..

this happens because the change detections have finished by then, so if you must change anything at that point, you need to call explicitly for a new change detection cycle via `detectChanges`
  • Loading branch information
emoralesb05 authored Jun 12, 2017
1 parent 349d108 commit 2606b75
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/app/components/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, HostBinding, AfterViewInit } from '@angular/core';
import { TdMediaService } from '@covalent/core';
import { GitHubService } from '../../services';

import { fadeAnimation } from '../../app.animations';
Expand Down Expand Up @@ -72,12 +71,10 @@ export class HomeComponent implements AfterViewInit {
},
];

constructor(private _gitHubService: GitHubService,
public media: TdMediaService) {
constructor(private _gitHubService: GitHubService) {
}

ngAfterViewInit(): void {
this.media.broadcast();
this._gitHubService.queryStartCount().subscribe((starsCount: number) => {
this.starCount = starsCount;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,20 @@ <h3 class="md-title">HTML</h3>
<h3 class="md-title">Typescript</h3>
<td-highlight lang="typescript">
<![CDATA[
import { ChangeDetectorRef } from '@angular/core';
import { TdMediaService } from '@covalent/core';
...
export class MyComponent {

constructor(public media: TdMediaService) {}
constructor(private _changeDetectorRef: ChangeDetectorRef,
public media: TdMediaService) {}

ngAfterViewInit(): void {
// broadcast to all listener observables when loading the page
this.media.broadcast();
// force a new change detection cycle since change detections
// have finished when `ngAfterViewInit` is executed
this._changeDetectorRef.detectChanges();
}
}
]]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export class ManageListComponent implements AfterViewInit {

ngAfterViewInit(): void {
// broadcast to all listener observables when loading the page
Promise.resolve(undefined).then(() => {
this.media.broadcast();
this._changeDetectorRef.markForCheck();
});
this.media.broadcast();
this._changeDetectorRef.detectChanges();
}

}
7 changes: 6 additions & 1 deletion src/app/components/layouts/nav-list/nav-list.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,20 @@ <h3 md-line> Item Name </h3>
<h3 class="md-title">Typescript</h3>
<td-highlight lang="typescript">
<![CDATA[
import { ChangeDetectorRef } from '@angular/core';
import { TdMediaService } from '@covalent/core';
...
export class MyComponent {

constructor(public media: TdMediaService) {}
constructor(private _changeDetectorRef: ChangeDetectorRef,
public media: TdMediaService) {}

ngAfterViewInit(): void {
// broadcast to all listener observables when loading the page
this.media.broadcast();
// force a new change detection cycle since change detections
// have finished when `ngAfterViewInit` is executed
this._changeDetectorRef.detectChanges();
}
}
]]>
Expand Down
6 changes: 2 additions & 4 deletions src/app/components/layouts/nav-list/nav-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export class NavListComponent implements AfterViewInit {

ngAfterViewInit(): void {
// broadcast to all listener observables when loading the page
Promise.resolve(undefined).then(() => {
this.media.broadcast();
this._changeDetectorRef.markForCheck();
});
this.media.broadcast();
this._changeDetectorRef.detectChanges();
}

}

0 comments on commit 2606b75

Please sign in to comment.