我通過套接字連接從後端獲取更新。我想用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