<div scope-isolation reversename="reverseNameFn()"></div>

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.reverseNameFn = function(message){
     console.log(message);  //undefined
     return message;
  }
});

app.directive('scopeIsolation', function() {
  return{
    scope : {
       'reversename' : '&'
    },
    template : '<input type="text" ng-model="value"> {{value}} <button ng-click="reversename({message : value})">click me</button>',
    link : function(scope,elem,attrs){
         console.log("Inside scope isolation directive");
    }
  }
});


调用reversename({message:value})时,消息对象在函数中未定义。

Plnkr:http://plnkr.co/edit/c2hch5Kozr9yjRLWN6K9?p=preview

最佳答案

处理&时,表达式将被求值。您传递的表达式是reverseNameFn()

如果要动态传递变量,请绑定=

'reversename' : '='


在您的HTML中:

<div scope-isolation reversename="reverseNameFn"></div>

关于javascript - 指令函数中未传递参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35309998/

10-12 03:42