-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Isolate scope binding to parent function inconsistent context #8552
Comments
You're using the
If you use this correctly, you'll find that the |
And if I need to trigger the bound function not from ng-click, but from inside the link function/controller? Your plunkr is not the same in functionality as mine. |
see the previous revision, which would be the same functionality (just |
but then I can't pass arguments to the function, which defeats the purpose to do it programatically. the only way I found so far is to return a closure bound to the object: $scope.someInstanciatedClass = ...;
$scope.closure = function(){
return function(){
$scope.someInstanciatedClass.method(argumentsSomewhere);
};
}; <div some-directive="closure()"></div> that's a lot of code if I could just use |
you can pass any arguments you want to the function, using the locals object, which I demonstrated above! The expression gets evaluated within the context of the parent scope, so you can pass any name bound in the parent scope --- in addition to any bindings that you provide via the locals object. For instance, if I want to pass This whole issue seems to be a misunderstsanding about how these expression bindings work and what they're used for. It's not like C, you aren't passing the address of a function, and you don't need to care about it's signature. What you're actually doing is passing an expression which is evaluated within the context of the parent scope, and if that expression calls a function, then it calls a function. You can add local name bindings to that expression as I've shown, and this is used in the library to pass But yeah, it's not "pass a callback function", you can't expect it to behave as a callback function. When evaluating a function in an expression, yes it is bound to the context that we find it in --- if any --- but if you're evaluating the function yourself, then it's bound to whatever context you personally bind it to, and you have no way of knowing about where it ought to be bound to, so don't try. I hope that explains things a bit for you, it's pretty simple stuff. |
nope, this doesnt work, as I'm trying to explain: angular.module('stuff', []).controller('MyCtrl', function(){
this.func = function(data){ console.log(data, this); };
})
.directive('someDirective', function(){
return {
restrict: 'A',
scope: { 'fn': '&someDirective' },
template: '<div ng-click="doh()">Clicky</div>',
link: function(scope){
scope.doh = function(){
scope.fn({data: true});
};
}
};
}); |
that does actually work. |
so, in the end, just passing a closure can keep the context programatically, is the solution I've been using for a while for the context problem. |
it's just that you're looking at it the completely wrong way. When the parser calls a function, it will use either the object which contains the function (IE, the immediate parent context of the function), or the scope itself. When YOU invoke the passed function, you are responsible for calling it with the appropriate context. Angular can't make this magic happen for you, it doesn't work that way, that's just the laws of javascript. The solution is: stop thinking of This makes perfect sense, and it will work for your use case, but only if you stop imagining that |
When using an isolated scope, I find myself having to return a bound function from a
&
isolated scope binding, or thethis
becomes undefined or Window DOM.http://plnkr.co/edit/Dnhd67JxAnbcG9i21b3a?p=preview
The text was updated successfully, but these errors were encountered: