I'm using AngularJS to build HTML controls that interact with a legacy Flex application. All callbacks from the Flex app must be attached to the DOM window.
For example (in AS3)
ExternalInterface.call("save", data);
Will call
window.save = function(data){
// want to update a service
// or dispatch an event here...
}
From within the JS resize function I'd like to dispatch an event that a controller can hear. It seems that creating a service is the way to go. Can you update a service from outside of AngularJS? Can a controller listen for events from a service? In one experiment (click for fiddle) I did it seems like I can access a service but updating the service's data doesn't get reflected in the view (in the example an <option>
should be added to the <select>
).
Thanks!
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:
More safe and performant way especially when debug data is off is to use a shared variable to hold a callback function. Your angular controller implements this function to return its internals to the external code.
Generalized as a reusable directive :
I created a 'exposeScope' directive which works in a similar fashion but usage is simpler:
This stores the current scope ( that is given to the link function of the directive) in a global 'scopes' object which is a holder for all scopes. Value provided to the directive attribute is used as the property name of the scope in this global object.
See the demo here. As I showed in the demo, you can trigger jQuery events when the scope is stored and removed from the global 'scopes' object.
Note that, I haven't tested the on('scopeDestroyed') when the actual element is removed from the DOM. If it does not work, triggering the event on the document itself instead of the element may help. ( see the app.js ) script in the demo plunker.
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:
Thanks to the previous post, I can update my model with an asynchronous event.
I declare my model
And i update my model from outside my scope
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 elementangular.element(domElement).injector()
to get the current app injectorangular.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: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:
Now you can call your AngularJS code functions by way of the scope handle:
To make things more convenient, you can use a function to grab the scope handle anytime you want to access it:
Your calls would then look like this:
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