我通过套接字连接从后端获取更新。我想用AngularJS自动更新Frontend,同时使用数据对象作为我从后端得到的数据。如何在获取套接字更新时更新AngularJS模板?
我有什么?
模板:
Status: {{unit.getStatus()}}
控制器1:
function ($scope, unitFactory) {
// register to unit factory to get the updates and do
// $scope.unit = data.getUnit();
}
控制器2:
function ($scope, unitFactory) {
// register to unit factory to get the updates and do
// $scope.unit = data.getUnit();
// $scope.foo = data.getFoo();
}
服务:
function(requestFactory) {
var unit = {},
foo = {};
Sockets.socket('unit', function (response) {
unit = new Unit(response['data']);
foo = new Foo(response['foo']);
// This is the data object which has to be send to the controllers
var Data = {
getUnit: function() {
return unit;
},
getFoo: function() {
return foo;
}
// some more functions...
}
});
}
个
套接字:
channel.on('data', function (event) {
try {
event = JSON.parse(event);
// successCallback is what is given as second parameter in the `Service`.
$rootScope.$apply(successCallback(event));
} catch (e) {
console.log('Error: ' + e);
}
});
究竟应该如何共同努力?
- 插座更新进来,得到由
Sockets
对象 Sockets
呼叫其在Service
- 注册回调函数在
Service
处理数据 - MISSING的处理的功能处理包裹在物体中的数据必须传送到控制器
- MISSING只要有新更新,控制器就可以对数据执行任何操作。
- 该模板会自动更新。
任何人都可以帮我未命中部件?我尝试了很多不同的方法,但每次都跑到死路一条。
您是否尝试过使用事件而不是回叫? – sdfacre