0

I'm trying to run a function once clicking a button. Down is my current code but it doesn't work. This is not my main app.component, I'm trying to do this in my second tab.

add-profile.component.html

<button ng-click="myFunc()">Click</button>

add-profile.components.ts

export class AddProfileComponent implements OnInit {

  constructor() {
    this.showWindow();
  }

  myFunc():void{
    console.log("Works");
  }

  ngOnInit() {

  }
}

1 Answer 1

2

If you are using angular 2, you can't use ng-click. use (click)

<button (click)="myFunc()">Click</button>

Also, implement the showWindow function

export class AddProfileComponent implements OnInit {

  constructor() {
    this.showWindow();
  }
  showWindow(){
    // your code 
  }
  myFunc():void{
    console.log("Works");
  }

  ngOnInit() {

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

2 Comments

Thanks this worked! Does this reffer to all the existing ng commands? or just this one (when using Angular 2 (I'm using angular CLI)).
Angular 2 is different from angular 1 implementation. you have to go through the documentation properly. And yes its different

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.