user2552863
user2552863

Reputation: 499

error typeerror _co.onclick is not a function - Angular 2

I get ERROR TypeError: _co.onClick is not a function error with angular 2.

<span
    class="glyphicon"
    [class.glyphicon-star]="isFavorite"
    [class.glyphicon-star-empty]="!isFavorite"
    (click)="onClick()">
</span>

When user click the star that it should be color or empty. then my component ts file is here

@Component({
  selector: 'favorite',
  templateUrl: './favorite.component.html',
  styleUrls: ['./favorite.component.css']
})

Upvotes: 8

Views: 11445

Answers (2)

ayushi mundra
ayushi mundra

Reputation: 41

I had the same problem even after declaring the function in my component.So I changed the function name and it worked.

Upvotes: 4

gyc
gyc

Reputation: 4360

You need to declare that onClick function in your component.

    @Component({
      selector: 'favorite',
      templateUrl: './favorite.component.html',
      styleUrls: ['./favorite.component.css']
    })

    export class FavoriteComponent {

      constructor() { }

      onClick() {
        console.log("star clicked");
      }
   }

Upvotes: 5

Related Questions