Sphere Decorator

Is it possible, and if so, how to decorate $scope so that all areas have some additional function / property?

I am trying to do this:

 $provide.decorator('$scope', function($scope) { $scope.cakes = true; return $scope; }); 

But it explodes with:

Unknown provider: $scopeProvider from application.

I know that I can add properties and functions to $rootScope , and it will prototype inherit, but I want isolated areas in directives to also have access to these added things.

+4
source share
2 answers

I had the same problem.

Just skip the $ rootScope prototype. Then the selected areas will also have this method.

This is my attempt to use the debug lodash function as a native scope method:

 angular.module('Test', []) .config(function($provide) { $provide.decorator('$rootScope', function ($delegate) { $delegate.__proto__.$$busy = 0; $delegate.__proto__.$watchDebounce = function (watchExpression, listener, objectEquality){ var _scope = this; var debouncedListener = _.debounce(function (newValue, oldValue, scope){ listener(newValue, oldValue, scope); _scope.$$busy = 0; scope.$digest(); }, 1000); var wrappedListener = function (newValue, oldValue, scope){ _scope.$$busy = 1; debouncedListener(newValue, oldValue, scope); } return this.$watch(watchExpression, wrappedListener, objectEquality); } return $delegate; }) }) 

A working example is here http://jsfiddle.net/3ncct/

+9
source

This is not possible, but I would say that the entire area object is isolated .

What you can do is access the decorated materials via scope.$root , however for many use cases it will beat the target because the added functions will still only access $rootScope instead of your isolated one.

0
source

Source: https://habr.com/ru/post/1481317/


All Articles