Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating scope from outside AngularJs without using "angular.element.scope"

I'd like to be able to update a scope in Angular from a function outside Angular.

E.g., if I have a jQuery plugin which returns a success callback, I'd like to be able to update the scope from that success callback. Every solution I've seen for this involves calling angular.element(selector).scope and then calling $apply on the scope that is returned. However, I've also seen many comments indicating that this doesn't work when debug info is off and thus it isn't recommended, but I haven't seen any alternative solutions.

Does anyone know of a way to update the scope from outside of Angular without using angular.element(selector).scope?

Here is the accepted solution in the post:

"You need to use $scope.$apply() if you want to make any changes to a scope value from outside the control of AngularJs like a jQuery/javascript event handler.

function change() {
    alert("a");
    var scope = angular.element($("#outer")).scope();
    scope.$apply(function(){
        scope.msg = 'Superhero';
    });
}

Here is a warning that .scope() doesn't work when debug data is off in the post:

"FYI according to the docs using .scope() requires the Debug Data to be enabled but using Debug Data in production is not recommended for speed reasons. The solutions below seem to revolve around scope() – rtpHarry Dec 5 '14 at 15:12 "

I don't see any alternative solution to using .scope() in this post or in other similar posts.

AngularJS access scope from outside js function

Thanks!

Update One possible solution to not using angular.element(selector).scope was I assigned the scope in the controller I was using FirstCtrl to the window object. I injected $window into the FirstCtrl controller and did the following:

$window.FirstCtrlScope = $scope;

Then from jQuery or other javascript I could do:

var scope=window.FirstCtrlScope;
scope.$apply(function () {
      // update the scope
});

Is this a good solution or are there better solutions for updating a scope without using angular.element(selector).scope?

Thanks!

like image 864
user1164116 Avatar asked Jul 18 '15 06:07

user1164116


People also ask

What is the difference between $scope and scope in angular?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.

Why $scope is used in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

What is the difference between $scope and this in AngularJS?

"How does this and $scope work in AngularJS controllers?" When the controller constructor function is called, this is the controller. When a function defined on a $scope object is called, this is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on.

Does AngularJS support scope inheritance?

The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes.


Video Answer


2 Answers

I think both ways are bad. Design which sets controllers as global variables, or access to scope by html element leads to unmaintainable application with many hidden links.

If you need cooperate with jQuery plugins (or other non-angular code), wrap it into directive with clear API (attributes, bindings and callbacks).

like image 81
milanlempera Avatar answered Oct 02 '22 15:10

milanlempera


You can assign the scope to a data attribute on the element, then access that. Take a look here where the angular-ui-bootstrap library implemented that approach.

like image 40
Rob J Avatar answered Oct 02 '22 14:10

Rob J