首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角:通过工厂变量更新控制器作用域变量

问角:通过工厂变量更新控制器作用域变量
EN

Stack Overflow用户
提问于 2016-06-21 18:20:42
回答 3查看 65关注 0票数 0

我查看了如何正确地完成此操作的示例,但它肯定没有在我的终端上进行更新。我放置了一个断点,以确保它正在更新并通过工厂中的计时器,并且它正在正确地更新。我不应该用$watch对吧?如果有人能帮我弄清楚是怎么回事,现在就能治好我的头痛,谢谢。

工厂

代码语言:javascript
复制
app.factory('FoundationSystemStatusFactory', ['$timeout', '$q', 'SystemStatusFactory', function ($timeout, $q, SystemStatusFactory) {
var service = {};

service.Count = 0;

service.Ping = 0;

service.PollingTest = function() {
    $timeout(function () {

        SystemStatusFactory.PingIP('www.google.com')
            .then(function (data) {
                service.Ping = data.data;
                service.Count++;
            }, function (data) {
                service.Ping = data.data;
            });

        service.PollingTest();
    }, 2000);
}

return service;

}]);

控制器

代码语言:javascript
复制
 FoundationSystemStatusFactory.PollingTest();

 $scope.ping = FoundationSystemStatusFactory.Ping;  //NOT UPDATING

 $scope.count = FoundationSystemStatusFactory.Count;  //NOT UPDATING

编辑:尝试作为服务,仍然无法使它工作:

代码语言:javascript
复制
var self = this;

self.Count = 0;

self.Ping = 0;

self.PollingTest = function () {
    $timeout(function () {

        SystemStatusFactory.PingIP('www.google.com')
            .then(function (data) {
                self.Ping = data.data;
                self.Count++;
            }, function (data) {
                self.Ping = data.data;
            });

        self.PollingTest();
    }, 2000);
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-21 18:34:46

你可以使用观察者:

代码语言:javascript
复制
$scope.$watch('FoundationSystemStatusFactory.Ping', function(newValue) {
    $scope.ping = newValue;
});

或者您可以使用引用工厂:

代码语言:javascript
复制
$scope.status = FoundationSystemStatusFactory;

$interval(function() {
    console.log($scope.status.Ping);    // gets updated
});    
票数 1
EN

Stack Overflow用户

发布于 2016-06-21 19:13:59

另一种方法-- events。

代码语言:javascript
复制
app.factory('FoundationSystemStatusFactory', ['$rootScope', '$timeout', '$q', 'SystemStatusFactory', function ($rootScope, $timeout, $q, SystemStatusFactory) {
var service = {
 Count: 0
};

service.PollingTest = function() {
    $timeout(function () {
        SystemStatusFactory.PingIP('www.google.com')
            .then(function (data) {
                $rootScope.$broadcast('FoundationSystemStatus:ping', data.data);
                service.Count++;
            }).catch(function (data) {
                $rootScope.$broadcast('FoundationSystemStatus:ping', data.data);
            });

        service.PollingTest();
    }, 2000);
}

return service;

}]);

//On controller...
$scope.$on('FoundationSystemStatus:ping', function(ping){
 $scope.ping = ping;
});
票数 2
EN

Stack Overflow用户

发布于 2016-06-21 20:21:32

好吧,经过进一步的研究,我找到了该怎么做。对象以数字和字符串的形式引用。

工厂

代码语言:javascript
复制
app.factory('FoundationSystemStatusFactory', ['$timeout', '$q', 'SystemStatusFactory', function ($timeout, $q, SystemStatusFactory) {
var service = {};

service.Data = {
    Count: 0,
    Ping: 0
}

service.PollingTest = function() {
    $timeout(function () {

        SystemStatusFactory.PingIP('www.google.com')
            .then(function (data) {
                service.Data.Ping = data.data;
                service.Data.Count++;
            }, function (data) {
                service.Data.Ping = data.data;
            });

        service.PollingTest();
    }, 2000);
}

return service;
}]);

控制器

代码语言:javascript
复制
app.controller('SystemStatusController', ['$scope', '$rootScope', '$timeout', 'FoundationSystemStatusFactory',
    function ($scope, $rootScope, $timeout, FoundationSystemStatusFactory) {

        FoundationSystemStatusFactory.PollingTest();

        $scope.data = FoundationSystemStatusFactory.Data;
}]);

视图

代码语言:javascript
复制
{{data.Ping}}
{{data.Count}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37951818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档