Javascript 角度:通过工厂变量更新控制器范围变量
我研究了如何正确地实现这一点的示例,但它肯定不会在我这方面更新。我设置了一个断点,以确保它正在更新,并通过工厂中的计时器,并且它正在正确更新。我不应该用$watch,对吗?如果有人能帮我弄清楚到底发生了什么,这将有助于我现在的头痛哈哈,谢谢 工厂Javascript 角度:通过工厂变量更新控制器范围变量,javascript,angularjs,Javascript,Angularjs,我研究了如何正确地实现这一点的示例,但它肯定不会在我这方面更新。我设置了一个断点,以确保它正在更新,并通过工厂中的计时器,并且它正在正确更新。我不应该用$watch,对吗?如果有人能帮我弄清楚到底发生了什么,这将有助于我现在的头痛哈哈,谢谢 工厂 app.factory('FoundationSystemStatusFactory', ['$timeout', '$q', 'SystemStatusFactory', function ($timeout, $q, SystemStatusFac
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;
}]);
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;
}]);
控制器
FoundationSystemStatusFactory.PollingTest();
$scope.ping = FoundationSystemStatusFactory.Ping; //NOT UPDATING
$scope.count = FoundationSystemStatusFactory.Count; //NOT UPDATING
app.controller('SystemStatusController', ['$scope', '$rootScope', '$timeout', 'FoundationSystemStatusFactory',
function ($scope, $rootScope, $timeout, FoundationSystemStatusFactory) {
FoundationSystemStatusFactory.PollingTest();
$scope.data = FoundationSystemStatusFactory.Data;
}]);
编辑:已尝试作为服务,但仍无法使其工作:
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);
}
您可以使用watcher:
$scope.$watch('FoundationSystemStatusFactory.Ping', function(newValue) {
$scope.ping = newValue;
});
或者您可以使用工厂参考:
$scope.status = FoundationSystemStatusFactory;
$interval(function() {
console.log($scope.status.Ping); // gets updated
});
您可以使用watcher:
$scope.$watch('FoundationSystemStatusFactory.Ping', function(newValue) {
$scope.ping = newValue;
});
或者您可以使用工厂参考:
$scope.status = FoundationSystemStatusFactory;
$interval(function() {
console.log($scope.status.Ping); // gets updated
});
不同的方法-事件
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;
});
不同的方法-事件
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;
});
好的,我在做了更多的研究后才知道怎么做。对象被引用为数字,而字符串不被引用 工厂
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;
}]);
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;
}]);
控制器
FoundationSystemStatusFactory.PollingTest();
$scope.ping = FoundationSystemStatusFactory.Ping; //NOT UPDATING
$scope.count = FoundationSystemStatusFactory.Count; //NOT UPDATING
app.controller('SystemStatusController', ['$scope', '$rootScope', '$timeout', 'FoundationSystemStatusFactory',
function ($scope, $rootScope, $timeout, FoundationSystemStatusFactory) {
FoundationSystemStatusFactory.PollingTest();
$scope.data = FoundationSystemStatusFactory.Data;
}]);
看法
好的,我在做了更多的研究后才知道怎么做。对象被引用为数字,而字符串不被引用 工厂
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;
}]);
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;
}]);
控制器
FoundationSystemStatusFactory.PollingTest();
$scope.ping = FoundationSystemStatusFactory.Ping; //NOT UPDATING
$scope.count = FoundationSystemStatusFactory.Count; //NOT UPDATING
app.controller('SystemStatusController', ['$scope', '$rootScope', '$timeout', 'FoundationSystemStatusFactory',
function ($scope, $rootScope, $timeout, FoundationSystemStatusFactory) {
FoundationSystemStatusFactory.PollingTest();
$scope.data = FoundationSystemStatusFactory.Data;
}]);
看法
“你试过用服务代替工厂吗?”@Medttley不,让我现在就试试。我一直在查找两者之间的差异。。除了它的实例化方式,我仍然对两者之间的区别感到困惑。简单地说,工厂返回数据,服务修改数据。不仅仅是将app.factory更改为app.service,你还需要更改逻辑,阅读一些文档,这非常重要easy@MedetTleukabiluly好的,调查一下,谢谢你尝试使用服务而不是工厂?@medttleucabilly不,让我现在就试试。我一直在查找两者之间的差异。。除了它的实例化方式,我仍然对两者之间的区别感到困惑。简单地说,工厂返回数据,服务修改数据。不仅仅是将app.factory更改为app.service,你还需要更改逻辑,阅读一些文档,这非常重要easy@MedetTleukabiluly好的,调查一下,谢谢你的tipi,知道$watch的工作原理,但我相信有一个正确的方法来实现我想要的,而不必使用$watch?谢谢你的支持answer@user1189352你看到我的第二种方法了吗?这对你有用吗?嗨,对不起,是的,我有。我相信这也行得通,如果我不能让它发挥作用,我会用你的方法。我只是不想“破解”它,而是想用正确的方法学习/做它。。我确信我应该能够在不必使用$interval的情况下使其工作。我包含的
$interval
仅用于演示目的。问题是,当您编写时,$scope.status=FoundationSystemStatusFactory代码><代码>$sope.status
包含对工厂的引用。当factory属性更改时,引用包含新的未复制数据。因为基本上引用是指向存储单元的链接。当您编写$scope.ping=FoundationSystemStatusFactory.ping代码><代码>$scope.ping
包含一个值(不是引用)。之所以发生这种情况,是因为FoundationSystemStatusFactory.Ping=='number'
的类型。感谢您的解释!有助于透视事物我知道$watch是有效的,但我相信有一种不必使用$watch就能实现我想要的东西的正确方法?谢谢你的支持answer@user1189352你看到我的第二种方法了吗?这对你有用吗?嗨,对不起,是的,我有。我相信这也行得通,如果我不能让它发挥作用,我会用你的方法。我只是不想“破解”它,而是想用正确的方法学习/做它。。我确信我应该能够在不必使用$interval的情况下使其工作。我包含的$interval
仅用于演示目的。问题是,当您编写时,$scope.status=FoundationSystemStatusFactory代码><代码>$sope.status
包含对工厂的引用。当factory属性更改时,引用包含新的未复制数据。因为基本上引用是指向存储单元的链接。当您编写$scope.ping=FoundationSystemStatusFactory.ping代码><代码>$scope.ping
包含一个值(不是引用)。之所以发生这种情况,是因为FoundationSystemStatusFactory.Ping=='number'
的类型。感谢您的解释!这基本上就是我给你的建议。还有,很抱歉,“对象被引用为数字,字符串被引用为非数字。”这句话毫无意义。@goliney好的,我给了你答案,那基本上就是我给你的建议。还有,很抱歉,“对象被引用为数字,字符串则不是。”这句话毫无意义。@Golliney好的,我当时给了你答案