I am trying to enable /disable button
App.component.html : Root component triggers on change and sets initial value.
<!--The content below is only a placeholder and can be replaced.-->
<div class="container">
<div style="text-align:center">
<h1>
{{ title }}!
</h1>
<favorite [isFavorite]="post.isFavorite" (change)="onFavoriteChange($event)" ></favorite>
</div>
<h1></h1>
<courses></courses>
</div>
app.component.ts : consists of root component consists on change function and post object
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Hello User';
post={
title:'Pti',
isFavorite:true
}
onFavoriteChanged(isFav)
{
console.log("On Favorite Changed",isFav);
}
}
Favorite Component
favorite.component.ts
<button class="waves-effect waves-light btn" [class.enabled]="isSelected"
[class.disabled]= "!isSelected"
(click)="onClick" >Click to {{isSelected}}</button>
favorite.component.ts : consists of onclick to toggled the class and emit the result
import { Component, OnInit, Input, Output ,EventEmitter } from '@angular/core';
@Component({
selector: 'favorite',
templateUrl: './favorite.component.html',
styleUrls: ['./favorite.component.css']
})
export class FavoriteComponent implements OnInit {
@Input('isFavorite') isSelected: boolean;
@Output() change=new EventEmitter();
onClick()
{
console.log("CLicked");
this.isSelected=!this.isSelected;
this.change.emit(isSelected);
}
ngOnInit() {
}
}
The button is not toggling. No class change and emitter too does not emit any result.
(click)="onClick"to(click)="onClick()"in yourfavorite.component.ts