Việc cập nhật thuộc tính mô hình không ảnh hưởng đến chế độ xem khi cập nhật mô hình trong gọi lại sự kiện, bất kỳ ý tưởng nào để sửa lỗi này?Chế độ xem không được cập nhật trong AngularJS
Đây là dịch vụ của tôi:
angular.service('Channel', function() {
var channel = null;
return {
init: function(channelId, clientId) {
var that = this;
channel = new goog.appengine.Channel(channelId);
var socket = channel.open();
socket.onmessage = function(msg) {
var args = eval(msg.data);
that.publish(args[0], args[1]);
};
}
};
});
publish()
chức năng được bổ sung động trong bộ điều khiển.
Bộ điều khiển:
App.Controllers.ParticipantsController = function($xhr, $channel) {
var self = this;
self.participants = [];
// here publish function is added to service
mediator.installTo($channel);
// subscribe was also added with publish
$channel.subscribe('+p', function(name) {
self.add(name);
});
self.add = function(name) {
self.participants.push({ name: name });
}
};
App.Controllers.ParticipantsController.$inject = ['$xhr', 'Channel'];
Xem:
<div ng:controller="App.Controllers.ParticipantsController">
<ul>
<li ng:repeat="participant in participants"><label ng:bind="participant.name"></label></li>
</ul>
<button ng:click="add('test')">add</button>
</div>
Vì vậy, vấn đề là cách nhấn vào nút cập nhật xem đúng, nhưng khi tôi nhận được thông báo từ nothings Kênh xảy ra, ngay cả những add()
chức năng được gọi là
Điều này hữu ích và một khi tôi nghĩ về lý do tại sao tôi ha d để làm điều này trong trường hợp các sự kiện DOM/jQuery, nó có ý nghĩa với tôi và tôi đã có thể chấp nhận nó. – duma
Tuyệt. Tôi không biết bạn có thể tiêm vào phạm vi gốc. – heneryville
Đối với các cuộc gọi lại 'setTimeout', sử dụng dịch vụ' $ timeout' thay vào đó: https://coderwall.com/p/udpmtq – itghisi