Javascript 角度:在控制器之间共享异步服务数据

Javascript 角度:在控制器之间共享异步服务数据,javascript,angularjs,asynchronous,service,factory,Javascript,Angularjs,Asynchronous,Service,Factory,我想在控制器之间“绑定异步数据的更改” 我知道这可能有点让人困惑,但我希望这是可能的 在下面的示例中,如果我在输入中写入了一些内容,那么效果非常好: HTML: <div ng-app="myApp"> <div ng-controller="ControllerA"> ControllerA.message = {{message.hello}}<br/> <input type="text" ng-model=

我想在控制器之间“绑定异步数据的更改”

我知道这可能有点让人困惑,但我希望这是可能的

在下面的示例中,如果我在输入中写入了一些内容,那么效果非常好:

HTML

<div ng-app="myApp">
    <div ng-controller="ControllerA">
        ControllerA.message = {{message.hello}}<br/>
        <input type="text" ng-model="message.hello"/>
    </div>
    <hr/>
    <div ng-controller="ControllerB">
        ControllerB.message = {{message.hello}}<br/>
        <input type="text" ng-model="message.hello"/>       
    </div>
</div>
<div ng-app="myApp">
    <div ng-controller="ControllerA">
        ControllerA.message = {{message.hello}}<br/>
        <input type="text" ng-model="message.hello"/>
    </div>
    <hr/>
    <div ng-controller="ControllerB">
        ControllerB.message = {{message.hello}}<br/>
        <input type="text" ng-model="message.hello"/>       
    </div>
</div>

但假设我从服务器获取数据。我想像前面的例子一样“链接”数据

问题是,我希望避免使用“$broadcast”/“$on”或在$rootScope中共享对象

HTML

angular.module('myApp', [])
    .factory('myService', function($q, $timeout) {
        var message = {
            hello: 'hello world'
        };
        return {
            getMessage : function(){
                return message;
            }
       };
    })

function ControllerA($scope, myService) {
    $scope.message = myService.getMessage();
}

function ControllerB($scope, myService) {
    $scope.message = myService.getMessage();
}
angular.module('myApp', [])
    .factory('myService', function($q, $timeout) {
        var message = {};
        return {
            getMessage : function(){
                var deferred = $q.defer();

                $timeout(function() {
                    message.hello = 'Hello world!';
                    deferred.resolve(message);
                }, 2000);

                return deferred.promise;  
            }
       };
    })

function ControllerA($scope, myService) {
    $scope.message = myService.getMessage();
}

function ControllerB($scope, myService) {
    $scope.message = myService.getMessage();
}
谢谢你的帮助


Victor

您返回的是
工厂
的返回对象中的
承诺
,而不是实际对象本身。因此,在您的范围内,您实际上应该等待修复具体对象的承诺,然后将其分配给
$scope.message

例如:

function ControllerA($scope, myService) {
     myService.getMessage().then(function(obj){
              $scope.message=obj
             });
}

我将您的小提琴改为可能是您的答案的东西,请参见小提琴

您在
工厂
的返回对象中返回的是
承诺
,而不是实际的对象本身。因此,在您的范围内,您实际上应该等待修复具体对象的承诺,然后将其分配给
$scope.message

例如:

function ControllerA($scope, myService) {
     myService.getMessage().then(function(obj){
              $scope.message=obj
             });
}

我把你的小提琴改成了可能是你答案的东西,请看小提琴

我的答案在哪里??我想我已经回答了。@sza我感谢您的帮助,但在大规模应用程序中使用rootScope或$broadcast/$on并不容易。我的答案在哪里??我想我已经回答了。@sza我感谢您的帮助,但在大规模应用程序中使用rootScope或$broadcast/$on并不是一件容易的事。尽管您必须记住,这种方法只是,只有当您希望对某个对象进行赋值,而不是对该对象的某些属性进行赋值时,才可用。这是否真的回答了问题?这不只是更新ControllerA范围的消息吗?如何在两个作用域中更新消息变量?@flyingL123您可以在
ControllerB
中对
.then()
执行相同的链接。由于Angular服务是单例的,两个控制器将访问同一个实例的
myService
。尽管您必须记住,这个方法是唯一可用的,而且我强调,只有当您希望对一个对象进行赋值,而不是对该对象的某个属性进行赋值时,这个方法才能真正回答问题吗?这不只是更新ControllerA范围的消息吗?如何在两个作用域中更新消息变量?@flyingL123您可以在
ControllerB
中对
.then()
执行相同的链接。由于Angular服务是单例的,所以两个控制器将访问同一个
myService
实例。