Rob
Rob

Reputation: 738

AngularJS $apply inside scope

Is there another approach which could be used to carry out the following? This jQuery function is used inside the Angular $scope, and it works fine however it throws an $apply error, essentially because it's seen as an $apply inside an $apply. Yet if I remove $scope.$apply() line it stops working.

function myfunction(start, end) {
        // Lots of jQuery code here - omitted from this example

        // Update the scope 
        $scope.myf = "abc";
        $scope.myt = "def";
        $scope.$apply();
}

myfunction();

Upvotes: 0

Views: 57

Answers (2)

Asqan
Asqan

Reputation: 4479

use $scope.$evalAsync() instead. it would not throw an error and do the same in this case.

Upvotes: 0

Walfrat
Walfrat

Reputation: 5353

Either wrap your code in a $timeout or use $apply(). Note that $timeout is calling $apply() internally. $apply is specially made to resync external changes (not in angular loop) with angular.

If your function trigger on click or on event. You can bind your function to angular by using the directive ng-click or ng-[event].

Upvotes: 1

Related Questions