Open roypeled opened 11 years ago
I believe this is because the isolate scope of scoped directives doesn't inherit from $rootScope
. I'm actually interested in how this could be avoided because I just finished writing a Scope.$watchOnce
module which adds $watchOnce
to the $rootScope
and I want to be able to use it on isolate scopes.
Right now, this is what I'm doing:
angular.bind($scope, $scope.$root.$watchOnce)('expression', watcherHandler);
But I'd much rather just more simply do:
$scope.$watchOnce('expression', watchHandler);
Any idea how this could be accomplished in an isolate scope context @matsko?
You could add it as a service too and inject it as a dependency in the directive. Take a look at how others did it: https://github.com/cotag/angular-safeapply/blob/master/safe-apply.js
Decided to hijack $new
like this. Works great...
var scopePrototype = Object.getPrototypeOf($rootScope);
var oldNew = scopePrototype.$new;
// hijack the prototype's $new
scopePrototype.$new = function $new() {
var scope = oldNew.apply(this, arguments);
addFunctionality(scope);
return scope;
};
Awesome! Thanks for the fix!
Sorry @roypeled, I didn't fix @matsko's project, I'm talking about how I implemented a fix to the same issue on my project. I'm sure @matsko would love a PR if you want to implement the same fix in this repo...
Ah okay. I'll see if I have time, but good fix nonetheless.
It is usable only by accessing $root, like so: $scope.$root.$safeApply()