Call AngularJS from legacy code
Solution 1:
Interop from outside of angular to angular is same as debugging angular application or integrating with third party library.
For any DOM element you can do this:
-
angular.element(domElement).scope()
to get the current scope for the element -
angular.element(domElement).injector()
to get the current app injector -
angular.element(domElement).controller()
to get a hold of theng-controller
instance.
From the injector you can get a hold of any service in angular application. Similarly from the scope you can invoke any methods which have been published to it.
Keep in mind that any changes to the angular model or any method invocations on the scope need to be wrapped in $apply()
like this:
$scope.$apply(function(){
// perform any model changes or method invocations here on angular app.
});
Solution 2:
Misko gave the correct answer (obviously), but some of us newbies may need it simplified further.
When if comes to calling AngularJS code from within legacy apps, think of the AngularJS code as a "micro app" existing within a protected container in your legacy application. You cannot make calls to it directly (for very good reason), but you can make remote calls by way of the $scope object.
To use the $scope object, you need to get the handle of $scope. Fortunately this is very easy to do.
You can use the id of any HTML element within your AngularJS "micro-app" HTML to get the handle of the AngularJS app $scope.
As an example, let's say we want to call a couple of functions within our AngularJS controller such as sayHi() and sayBye(). In the AngularJS HTML (view) we have a div with the id "MySuperAwesomeApp". You can use the following code, combined with jQuery to get the handle of $scope:
var microappscope = angular.element($("#MySuperAwesomeApp")).scope();
Now you can call your AngularJS code functions by way of the scope handle:
// we are in legacy code land here...
microappscope.sayHi();
microappscope.sayBye();
To make things more convenient, you can use a function to grab the scope handle anytime you want to access it:
function microappscope(){
return angular.element($("#MySuperAwesomeApp")).scope();
}
Your calls would then look like this:
microappscope().sayHi();
microappscope().sayBye();
You can see a working example here:
http://jsfiddle.net/peterdrinnan/2nPnB/16/
I also showed this in a slideshow for the Ottawa AngularJS group (just skip to the last 2 slides)
http://www.slideshare.net/peterdrinnan/angular-for-legacyapps
Solution 3:
Greatest explanation of the concept I've found is situated here: https://groups.google.com/forum/#!msg/angular/kqFrwiysgpA/eB9mNbQzcHwJ
To save you the clicking:
// get Angular scope from the known DOM element
e = document.getElementById('myAngularApp');
scope = angular.element(e).scope();
// update the model with a wrap in $apply(fn) which will refresh the view for us
scope.$apply(function() {
scope.controllerMethod(val);
});
Solution 4:
Further to the other answers. If you don't want to access a method in a controller but want to access the service directly you can do something like this:
// Angular code* :
var myService = function(){
this.my_number = 9;
}
angular.module('myApp').service('myService', myService);
// External Legacy Code:
var external_access_to_my_service = angular.element('body').injector().get('myService');
var my_number = external_access_to_my_service.my_number