The following controller works without problem.
app.controller('foo', ['$scope',function ($scope) {
$scope.delete = function(){
bar($scope);
}
}]);
I tried to make it a little cleaner by using bind:
app.controller('foo', ['$scope',function ($scope) {
$scope.delete = bar.bind(null, $scope);
}]);
Unfortunately, this form does not work as expected and $scope is always supplied with an old version of $scope in bound method (bar here), even after $scope has changed to refer to a different value. What is wrong with it?
What else?
If I should not use bind here, what is the alternative?
I assume that your problem is that your bound
Util.baris always supplied with an old version of$scope, even after$scopehas changed to refer to a different value.bindbinds values, not variables. You are binding the current value of$scopetoUtil.bar. On the other hand, your first style forces the identifier$scopeto be resolved to a value (or, really, outer-scope variable record) every time the function runs.If
$scopechanges to refer to a completely different value, you must use the first form..bind(null, $scope)will resolve$scopeto a value immediately and use that value forever, while the first form withoutbindwill resolve$scopeto a value every time the function runs.