0

i have a html button like this

<md-button type="button"  ng-click="commentDelete(item,$index)" aria-label="change address" >
<md-icon md-svg-icon="img/ic_highlight_remove_24px.svg"></md-icon>
</md-button>

this is my controller

var commentDelete = function(item,index){
   console.log('working')
} 

$scope.commentDelete =commentDelete;

this is working without any problem. but if i try something like this

var myCtrl= function(item,index){
   return {
      commentDelete : function(item,index){
        console.log('working')
      }
   }
} 

 $scope.commentDelete =myCtrl.commentDelete

then it doesn't trigger the commentDelete function. why is that? thanks in advance

1
  • I think, myCtrl must be object to use this convention (myCtrl.commentDelete). So, you should declare myCtrl as object. var myCtrl= { commentDelete : function(item,index){ console.log('working') } } Commented Oct 28, 2016 at 12:57

1 Answer 1

3

Because you assign to the $scope.commentDelete a property of the myCtrl(myCtrl is a function, and it tries to find a commentDelete on it or in the Function). myCtrl is only a function, which returns an object with the function commentDelete.

var myCtrl= function(item,index){
   return {
      commentDelete : function(item,index){
        console.log('working')
      }
   }
} 

 $scope.commentDelete = myCtrl.commentDelete

If you call myCtrl and then get the commentDelete, it will work

$scope.commentDelete = myCtrl().commentDelete;
Sign up to request clarification or add additional context in comments.

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.