Reputation: 5041
I know angular directive
very deeply, and I'm trying to do something very simple, and I think it's not supported in angular.
I'm trying to pass a function as argument to the directive isolated scope (type &
).
I just want to deliver the "@
" scope values to the function implemented in my controller.
It seems like it's impossible to call the "&
" scope attribute with arguments.
<div ng-app="MyApp">
<div ng-controller="MyController">
<testkeyvalue accept="blabla" key='keyA' value='valueA' />
</div>
</div>
var myApp = angular.module('MyApp', [])
myApp.directive('testkeyvalue', function ()
{
return {
restrict: 'E',
replace: true,
scope: {
key: '@',
value: '@',
accept: "&"
},
template: '<div><label class="control-label">{{key}}</label><br/>' +
'<label class="control-label">{{value}}</label>' ,
link: function (scope, element, attrs)
{
var arr = ["key", "value", "accept"];
for (var i = 0, cnt = arr.length; i < arr.length; i++)
{
scope.$watch(arr[i], function ()
{
cnt--;
if (cnt <= 0)
{
fireaccept()
}
});
}
var fireaccept = function ()
{
//This is the problem
//I can't deliver this arguments to the "blabla" function im my controller
scope.accept(scope.key, scope.value);
}
}
};
});
myApp.controller('MyController', function ($scope, $window)
{
$scope.blabla = function (key, val)
{
$window.alert("key:" + key + " val: " + val);
};
});
Here is the full demo: http://jsfiddle.net/chezih/y85Ft/401/
I've read this question: AngularJS: How to pass arguments/functions to a directive?
But it's not what I'm looking for, I just want to register the controller, to event that triggered from inside the directive (and the event can pass arguments).
There is any way to do this?
Upvotes: 3
Views: 4826
Reputation: 498
As Jussi Kosunen said in the comments, you can use the "=" operator instead of "&" to get access to a function as a variable in your isolate scope.
A minimal directive could look like this:
directive('exDir', function () {
return {
scope: {isolateScopeFunction: "=fun"},
template: '<button ng-click="isolateScopeFunction(\'inside isolateScope\')">press</button>'
};
});
and you would use it like this:
<div ex-dir fun="outerScopeFunctionName"></div>
Upvotes: 0
Reputation: 2378
this is working like this :
http://jsfiddle.net/Pascalz/hr8zua7q/
<div ng-app="MyApp">
<div ng-controller="MyController">
<testkeyvalue accept="blabla(key, val)" key='keyA' value='valueA' />
</div>
</div>
var myApp = angular.module('MyApp', [])
myApp.directive('testkeyvalue', function ()
{
return {
restrict: 'E',
replace: true,
scope: {
key: '@',
value: '@',
accept: "&"
},
template: '<div><label class="control-label">{{key}}</label><br/>' +
'<label class="control-label">{{value}}</label>' ,
link: function (scope, element, attrs)
{
var arr = ["key", "value", "accept"];
for (var i = 0, cnt = arr.length; i < arr.length; i++)
{
scope.$watch(arr[i], function ()
{
cnt--;
if (cnt <= 0)
{
fireaccept()
}
});
}
var fireaccept = function ()
{
//This is the problem
//I can't deliver this arguments to the "blabla" function im my controller
scope.accept({key:scope.key, val:scope.value});
}
}
};
});
myApp.controller('MyController', function ($scope, $window)
{
$scope.blabla = function (key, val)
{
$window.alert("key:" + key + " val: " + val);
};
});
Upvotes: 3