2014-03-31 217 views
0

我可以传递字符串和对象,但由于某些原因,当我将回调函数添加到我作为可配置选项使用的作用域对象时,Angular将它们取出。如何将函数作为参数传递给AngularJS指令?

HTML:

<div ng-controller="DemoCtrl"> 
    scope.options = {{ options }} 
    <br><br> 
    <div my-directive="{{ options }}"></div> 
</div> 

JS:

var app = angular.module('demo2', []); 

app.controller('DemoCtrl', function($scope) { 
    $scope.options = { 
     test_str: 'test', 
     init_cb: function() { 
      alert('custom init callback'); 
     }, 
     apply_cb: function() { 
      alert('custom apply callback'); 
     } 
    }; 
}); 
app.directive('myDirective', ['$parse', function($parse) { 
    function link(scope, element, attrs) { 
     var options = attrs.myDirective; 
     scope.init(options); 
    } 

    return { 
     restrict: 'A', 
     transclude: true, 
     link: link, 
     controller: ['$scope', function($scope) { 
      $scope.defaults = { 
       init_cb: function() { 
        alert('default init callback'); 
       }, 
       apply_cb: function() { 
        alert('default apply callback'); 
       } 
      }; 
      $scope.settings = {}; 

      $scope.init = function(options) { 
       $scope.settings = $.extend({}, $scope.defaults, $scope.options); 

       // init cb. 
       $scope.settings.init_cb(); 

       // apply cb. 
       $scope.settings.apply_cb(); 
      }; 
     }] 
    }; 
}]); 

这里是一个小提琴:http://jsfiddle.net/revolunet/pHZNY/

感谢您的帮助。

+0

作为一个更新,我发现,如果我' console.log($ scope.options)'在'DemoCtrl'中,obj包含函数。 –

回答

0

您正在尝试使用attr,这只能是一个字符串,当你需要实际的变量只是传递到范围:

var app = angular.module('demo2', []); 

app.controller('DemoCtrl', function($scope) { 
    $scope.options = { 
     test_str: 'test', 
     init_cb: function() { 
      alert('custom init callback'); 
     }, 
     apply_cb: function() { 
      alert('custom apply callback'); 
     } 
    }; 
}); 
app.directive('myDirective', ['$parse', function($parse) { 


    return { 
     restrict: 'A', 
     transclude: true, 
     scope: { //use scope to pass in variables 
      options: '=myDirective' 
     }, 

     controller: ['$scope', function($scope) { 


      $scope.defaults = { 
       init_cb: function() { 
        alert('default init callback'); 
       }, 
       apply_cb: function() { 
        alert('default apply callback'); 
       } 
      }; 
      $scope.settings = {}; 

      $scope.init = function(options) { 
       $scope.settings = angular.extend({}, $scope.defaults, $scope.options); 

       // init cb. 
       $scope.settings.init_cb(); 

       // apply cb. 
       $scope.settings.apply_cb(); 
      }(); 
     }] 
    }; 
}]); 
+0

谢谢,这是更清洁,但我仍然有问题添加方法到'DemoCtrl'范围内的对象。将'console.log($ scope.options)'添加到'DemoCtrl'并显示方法。但是,如果您将表达式打印到HTML中的“{{$ scope.options}}”页面,则它将打印没有方法的对象。出于某种原因,Angular似乎将它们除去...... –

+0

为了呈现它,它将该对象转换为JSON字符串,并且JSON不包含函数。如果您只需要修改原始选项,请将范围设置为false。看到http://jsfiddle.net/pHZNY/8/ – dave

+0

我想创建一个简单的方法来扩展与自定义回调函数的默认选项,类似于一个jQuery插件的方式。如果我在指令中插入一个对象,它就会起作用。如果我使用$ scope模型并使用表达式将该值与HTML中的指令相关联,则它不起作用。我不知道为什么Angular剥离了$ scope模型中的函数。 –

相关问题