Call a function on click event in Angular 2

后端 未结 5 1586
难免孤独
难免孤独 2020-12-01 03:53

How to declare a function inside a component (typescript) and call it on a click event in Angular 2? Following is the code for the same functionality in Angular 1 for which

5条回答
  •  臣服心动
    2020-12-01 04:35

    The line in your controller code, which reads $scope.myFunc={ should be $scope.myFunc = function() { the function() part is important to indicate, it is a function!

    The updated controller code would be

    app.controller('myCtrl',['$scope',function($cope){
        $scope.myFunc = function() {
        console.log("function called");
      };
    }]);
    

提交回复
热议问题