Last active
April 26, 2018 08:08
-
-
Save jhades/db174e245e4e8b4f327bd86fe253806b to your computer and use it in GitHub Desktop.
Angular @ViewChild Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Choose Brand Colors:</h2> | |
<color-sample [color]="primary" #primaryColorSample> | |
</color-sample> | |
<mat-input-container> | |
<mat-label>Primary Color</mat-label> | |
<input matInput #primaryInput [(colorPicker)]="primary" [(ngModel)]="primary"/> | |
</mat-input-container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent { | |
..... | |
@ViewChild(ColorSampleComponent) | |
primarySampleComponent: ColorSampleComponent; | |
..... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements AfterViewInit { | |
..... | |
@ViewChild(ColorSampleComponent) | |
primarySampleComponent: ColorSampleComponent; | |
ngAfterViewInit() { | |
console.log('Values on ngAfterViewInit():'); | |
console.log("primaryColorSample:", this.primarySampleComponent); | |
} | |
..... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'color-sample', | |
template: ` | |
<div class="color-sample mat-elevation-z3" | |
[style.background-color]="color"> | |
<mat-icon>palette</mat-icon> | |
</div> | |
` | |
}) | |
export class ColorSampleComponent { | |
@Input() color; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements AfterViewInit { | |
.... | |
@ViewChild(MatIcon) | |
matIcon: MatIcon; | |
ngAfterViewInit() { | |
console.log('Values on ngAfterViewInit():'); | |
console.log("matIcon:", this.matIcon); | |
} | |
.... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2 #title>Choose Brand Colors:</h2> | |
... the rest of the AppComponent template ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements AfterViewInit { | |
..... | |
@ViewChild('title') | |
title: ElementRef; | |
ngAfterViewInit() { | |
console.log('Values on ngAfterViewInit():'); | |
console.log("title:", this.title.nativeElement); | |
} | |
..... | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Choose Brand Colors:</h2> | |
<color-sample | |
[color]="primary" | |
#primaryColorSample | |
(click)="open()"> | |
</color-sample> | |
... the rest of the AppComponent template ... | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements AfterViewInit { | |
.... | |
@ViewChild('primaryColorSample') | |
sample: ColorSampleComponent; | |
ngAfterViewInit() { | |
console.log('Values on ngAfterViewInit():'); | |
console.log("sample:", this.sample); | |
} | |
.... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent implements AfterViewInit { | |
.... | |
@ViewChild('primaryColorSample', {read: ElementRef}) | |
sample: ElementRef; | |
ngAfterViewInit() { | |
console.log('Values on ngAfterViewInit():'); | |
console.log("sample:", this.sample.nativeElement); | |
} | |
.... | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2 #title>Choose Brand Colors:</h2> | |
<color-sample [color]="primary" #primaryColorSample (click)="primaryInput.openDialog()"> | |
</color-sample> | |
<mat-input-container> | |
<mat-label>Primary Color</mat-label> | |
<input matInput #primaryInput [(colorPicker)]="primary" [(ngModel)]="primary"/> | |
</mat-input-container> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2 #title>Choose Brand Colors:</h2> | |
<color-sample [color]="primary" #primaryColorSample (click)="openColorPicker()"> | |
</color-sample> | |
<mat-input-container> | |
<mat-label>Primary Color</mat-label> | |
<input matInput #primaryInput [(colorPicker)]="primary" [(ngModel)]="primary"/> | |
</mat-input-container> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html' | |
}) | |
export class AppComponent { | |
primary = '#1976d2'; | |
@ViewChild('primaryInput', {read: ColorPickerDirective}) | |
colorPicker: ColorPickerDirective; | |
openColorPicker() { | |
this.colorPicker.openDialog(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment