从传统的code呼叫角JS传统、code、JS

2023-09-08 12:00:16 作者:深碍久绊凉

我使用的角度来构建与传统的Flex应用程序进行交互HTML控件。所有来自Flex应用回调必须连接到DOM窗口

I'm using angular to build HTML controls that interact with a legacy Flex application. All callbacks from the Flex app must be attached to the DOM window.

例如(在AS3)

ExternalInterface.call("save", data);

将调用

window.save = function(data){
    // want to update a service 
    // or dispatch an event here...
}

在JS中的调整功能,我想派遣一个事件,一个控制器可以听到。看来,建立一个服务是要走的路。你可以从更新的角度之外的服务?可以将一个控制器侦听来自服务的事件?在一个实验(点击小提琴)我做到了,好像我可以访问服务,但更新服务的数据不会反映在视图(在本例中的<选项> 应加入<选择> )。

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 Angular? 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>).

谢谢!

推荐答案

从互操作的角度之外的角度是一样的角度调试应用程序或与第三方库集成。

Interop from outside of angular to angular is same as debugging angular application or integrating with third party library.

对于任何DOM元素,你可以这样做:

For any DOM element you can do this:

angular.element(domElement).scope()来获取当前范围的元素 angular.element(domElement).injector()来获得当前应用注射器 angular.element(domElement).controller()来得到举行的 NG-控制器实例。 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 the ng-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.
});