Input in angular code example
Example 1: how to make input in angular optional
export class ChildComponent {
@Input() showName?: boolean;
constructor() { }
}
Example 2: output event angular
@Component({...})
export class CounterComponent {
@Input()
count: number = 0;
@Output()
change: EventEmitter<number> = new EventEmitter<number>();
increment() {
this.count++;
this.change.emit(this.count);
}
decrement() {
this.count--;
this.change.emit(this.count);
}
}
Example 3: input set variable angular
@Component({
selector: 'my-component',
template: '<h3> My component </h3>'
})
export class MyComponent {
@Input()
set name(str: string) {
this.service.setName(str);
console.log(str);
}
}
Example 4: input property angular
<app-slider [title]="'This is a title'"> </app-slider>
.
.
@input() title: string;
constructor(){
}
<div id='slider' class='slider-big'>
<h1> {{title}}</h1>
</div>