문자열과 객체를 전달할 수 있지만 구성 가능한 옵션으로 사용하고있는 범위 객체에 콜백 함수를 추가 할 때 Angular가이를 제거합니다.함수를 매개 변수로 AngularJS 지시문에 전달하려면 어떻게해야합니까?
HTML :
<div ng-controller="DemoCtrl">
scope.options = {{ options }}
<br><br>
<div my-directive="{{ options }}"></div>
</div>
JS : 당신의 도움에 대한 http://jsfiddle.net/revolunet/pHZNY/
감사 : 여기
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();
};
}]
};
}]);
는 바이올린입니다.
, 내가 발견 한 그 경우 I' console.log ($ scope.options)''DemoCtrl'에서 obj는 함수를 포함합니다. –