我想更改传递给指令的函数调用的参数。
HTML
<div direx onabc="a.b({'a':10,'b':13});" >
指令
.directive('direx', function($compile) {
return {
scope: {
onabc:'&onabc'
},
controller: function($scope,a){
$scope.a=a; //a service
//In controller of this directive which is called by ng-click
$scope.ngClicked = function() {
// WANT TO CHANGE A PARAM TO CALL OF abc()
$scope.value=100;
$scope.onabc({"b":$scope.value}); // should call a.b({a:10,b:100});
return result;
};
}
答案 0 :(得分:0)
由于您在控制器/范围中注入服务,因此无需将该功能传递给隔离范围。
<强> HTML 强>
<div direx onabc="{'a':10,'b':13}"></div>
<强>指令强>
.directive('direx', ['a', function(a){
return {
restrict: 'A',
scope: {
'onabc': '@'
},
template: '<div ng-click="ngClicked()">Click me {{ value }}</div>',
controller: ['$scope', function($scope){
$scope.ngClicked = function() {
// WANT TO CHANGE A PARAM TO CALL OF abc()
value = $scope.$eval($scope.onabc);
$scope.value = value.b = 100;
a.b(value); // should call a.b({a:10,b:100});
};
}]
};
}])
请参阅plnkr http://plnkr.co/edit/z7lpfoKCS2bR3bRzsuwB?p=preview
答案 1 :(得分:0)
&#34; B&#34;如果你想提供一个&#34; local&#34;你的对象必须是一个单独的参数。你的指令中的值。这是本地人在通过角度调用函数时的工作方式。换句话说,当你说:
$scope.onabc({"b":$scope.value});
你正在告诉有角度使用b的本地定义而不是另一个范围内存在的那个...实际上,你必须做一些重命名才能让它工作为&#34; b&#34 ;会替换你不想要的a.b ......让我们调用你的本地对象mydata。然后将mydata放入HTML中的指令(请参阅fiddle):
<div direx onabc="a.b({'a':10,'b':13}, mydata);" >
JavaScript的:
mod.service("a", function () {
//take in mydata as another parameter for locals data
this.b = function (original, mydata) {
alert("original a: " + original.a);
alert("original b: " + original.b);
alert("locals mydata: " + mydata.b);
}
});
mod.directive('direx', function($compile) {
return {
scope: {
onabc:'&onabc'
},
template: '<div><button type="button" ng-click="ngClicked()">Click Me</button></div>',
controller: function($scope,a) {
$scope.a=a; //a service
$scope.ngClicked = function() {
$scope.value=100;
//define values for mydata, replace b with $scope.value here.
$scope.onabc({mydata: {b: $scope.value}});
};
}
}
});
现在,如果您想向本地人添加更多数据,您可以这样做:
$scope.onabc({mydata: {a: 50, b: 100}});
现在mydata.a和mydata.b就是你通过你的指令推进的。