我是新來的,我不是英文的,所以請原諒我,如果我犯了一些錯誤。AngularJs ng-include和socket.io
我的問題是下列之一:我嘗試使用(在服務器側的NodeJS)angularjs和socket.io
所以我對客戶端:
<html ng-app="my_app">
<head> ... </head>
<body>
...
<div id='main_conteneur' ng-controller="CtrlTemplate">
<div ng-include="template.url"></div>
</div>
</body>
</html>
在LIB JS:
var my_app = angular.module('Quizz', []);
my_app.controller('CtrlTemplate', ['$scope', function ($scope) {
$scope.template = { name: 'template1.html' , url: 'template/template1.html' };
socket.on('charger_template', function (data) {
$scope.template = {name : 'template2.html' , url : 'template/template2.html'};
});
}]);
和服務器端裏面做一個插座:
io.sockets.emit('charger_template', data_to_send);
一切工作正常(socket.io通信,angularjs加載我的第一個模板),除了template2.html。我確實收到來自服務器的信息,我進入
socket.on('charger_template', function (data) {});
但模板沒有改變。
如果我做的console.log($範圍),我能看到的範圍,我可以看到的是,「對象」模板中包含「template2.html」
我也試過有模板的像清單當然下面,並改變模板與陣列,但它也不工作(我prefere保持第一個解決方案)
$scope.templates = [
{ name: 'template1.html' , url: 'template/template1.html' },
{ name: 'template1.html' , url: 'template/template1.html' }
];
$scope.template = $scope.templates[0];
我在做什麼錯在這裏?
我還沒有試過socket.io和角度,但如果調用'$範圍我會好奇$適用()'中設置'$ scope.template'後您的客戶端'socket.on'處理程序可能會強制更新。 –
請發佈您的模板之一!我懷疑他們是錯誤的。 – andrbmgi
Haaaaaaa謝謝! $ scope。$ apply();作品!我花了5天才找到解決方案!非常感謝 – shiminou