0

New! Save questions or answers and organize your favorite content.
Learn more.

How can I call a function of a controller in Angular from JQuery when using routing. This means there is no controller defined in the HTML! There are many answers out there for doing this when NOT using routing. For example:

angular.element(document.getElementById('yourControllerElementID')).scope().$apply();

But this won't work if you're using routing where the controller is defined in app.js instead of your HTML.

1 Answer 1

1

Try to get the scope defined on the ng-view element

angular.element($('[ng-view]')).scope()

and see if it works

3
  • I can get the scope by doing: var scope = angular.element($('body')).scope(); This is useful for accessing variables but I can't seem to access a function: scope.myFunction();
    – bscalable
    Jun 4, 2014 at 3:53
  • You need the exact scope, parent scope cannot access child scopes. Jun 4, 2014 at 4:01
  • Use batarang to see what element has the scope with the functions you are looking for Jun 4, 2014 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.