0

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();
2
  • _"it works fine however it throws an $apply error" - It cannot be both working and not working. Commented Mar 7, 2016 at 13:57
  • The output desired, is produced yet it throws an error. Perhaps I should re-word. Commented Mar 7, 2016 at 13:59

2 Answers 2

1

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].

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Thank you. Because the function was not tied to a jQuery event - this solution worked perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.