Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active April 26, 2018 08:08
Show Gist options
  • Save jhades/db174e245e4e8b4f327bd86fe253806b to your computer and use it in GitHub Desktop.
Save jhades/db174e245e4e8b4f327bd86fe253806b to your computer and use it in GitHub Desktop.
Angular @ViewChild Examples
<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>
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
.....
@ViewChild(ColorSampleComponent)
primarySampleComponent: ColorSampleComponent;
.....
}
@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);
}
.....
}
@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;
}
@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);
}
....
}
<h2 #title>Choose Brand Colors:</h2>
... the rest of the AppComponent template ...
@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);
}
.....
}
<h2>Choose Brand Colors:</h2>
<color-sample
[color]="primary"
#primaryColorSample
(click)="open()">
</color-sample>
... the rest of the AppComponent template ...
@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);
}
....
}
@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);
}
....
}
<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>
<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>
@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