AngularJS:我需要更新从外界的角度服务 [英] AngularJS: I need to update a service from outside of angular

查看:123
本文介绍了AngularJS:我需要更新从外界的角度服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做'玩家'服务,我需要更新时,Flash对象加载完成的服务。

I have a service called 'player' and I need to update the service when a flash object is finished loading.

mySongPlayer.factory('player', function() {

var isPlayerLoaded = false;
var playerHolder = '';

window.playerReady = function(thePlayer) {
    playerHolder = window.document[thePlayer.id];
    addListeners();
    isPlayerLoaded = true;
}

var flashvars = {
    file:"", 
    autostart:"true",
    skin: "/skins/glow/glow.zip",
}

var params = {
    allowfullscreen:"false", 
    allowscriptaccess:"always"
}

var attributes = {
    id:"player1",  
    name:"player1"                    
}

swfobject.embedSWF("/player.swf", "player_placeholder", "100%", "40", "9.0.115", false, flashvars, params, attributes);

var playObj;
return playObj || (playObj = {
    currentId: 'test', currentUrl: 'url', playerHolder: ''
});
});​

我知道如何使用访问服务

I know how to access the service using

angular.element(DOMElement).injector().get('player')

但同时,我需要更新的模块中已经创建的实例返回'球员'的新实例。有没有办法做到这一点?我只希望玩家的一个实例,但我需要从外部的JavaScript初始化。

but it returns a new instance of 'player' while I need to update the instance already created in the module. Is there a way to do this? I only want one instance of the player, but I need to initialize it from outside javascript.

推荐答案

好吧,我真的不能看到所有的你在做什么,但你大概1/2的方式存在。

Well, I can't really see all of what you're doing, but you're probably about 1/2 way there.

这里是什么我要描述工作普拉克

injector.get()应该返回相同的服务实例作为什么在你的应用程序。你可能只是看到让你的认为的你有一个不同的实例的问题。

injector.get() should be returning the same instance of Service as what's in your app. You're probably just seeing an issue that makes you think you have a different instance.

所以,你需要做的是:


  • 确保正是你在你的范围从您的服务将是一个的目标引用。如果它是一个原始类型,它不会是相同的,所以它不会更新。

  • 一定要得到的范围了你的棱角元素与 angular.element(一个DOMElement).scope(),然后调用 $申请( )就可以了。

  • Make sure that what you're putting in your scope from your service is an object reference. If it's a primitive type, it won't be the same reference so it won't update.
  • Be sure to get the scope off of your angular element with angular.element(DOMElement).scope() and then call $apply() on it.

这里的code:

app.controller('MainCtrl', function($scope, myService) {
  // Set a var on the scope to an object reference of the
  // (or from the) service.
  $scope.myService = myService;
});

app.factory('myService', function(){
  return {
    foo: 'bar'
  };
});

//do a little something to change the service externally.
setTimeout(function(){
  //get your angular element
  var elem = angular.element(document.querySelector('[ng-controller]'));

  //get the injector.
  var injector = elem.injector();

  //get the service.
  var myService = injector.get('myService');

  //update the service.
  myService.foo = 'test';

  //apply the changes to the scope.
  elem.scope().$apply();
}, 2000)

其他的想法:


  • 您可能要注入 $窗口进入你的服务,而不是使用窗口对象,要保持你的服务的可测性。

  • 系统指令可能是DOM操作一个更好的选择,如创建一个Flash影片播放器。

  • You probably want to inject $window into your service, rather than use the window object, to maintain the testability of your service.
  • A directive is probably a better choice for DOM manipulation such as the creation of a Flash movie player.

这篇关于AngularJS:我需要更新从外界的角度服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆