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?