0

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.

4
  • have you tried this.change.emit(); because emit is a function? Commented Feb 10, 2018 at 21:09
  • 3
    Change (click)="onClick" to (click)="onClick()" in your favorite.component.ts Commented Feb 10, 2018 at 21:10
  • @bryan60 yes please check edit Commented Feb 10, 2018 at 21:10
  • What about the console.log("clicked"), is it fired; and the button content {{isSelected}}, is it changed? Commented Feb 10, 2018 at 21:18

2 Answers 2

1

Change (click)="onClick" to (click)="onClick()"

in favorite.component.ts

This is the correct answer .

Thanks @Daniel for pointing it out.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not clear from the description if the onClick function is triggered at all, but as for adjusting classes in Angular setting it directly like [class.whatever]="expression" also won't work for me, instead I succeeded with this form:

<button [ngClass]="{ 'enabled': isSelected, 'disabled': !isSelected }">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.