简体   繁体   English

Angular.js-$ scope.variable不会自动更新

[英]Angular.js - $scope.variable doesn't auto update

I have the following code 我有以下代码

// Generated by CoffeeScript 1.6.3
(function() {
  "use strict";
  var chatModule;

  chatModule = angular.module('chatModule', ['diplomovaPraceFrontendApp']);

  chatModule.service('chatModuleService', function(UserService, socket) {
    var obj,
      _this = this;
    obj = {};
    obj.chats = [];
    obj.initiate = function(from) {
      if (!_.has(obj.chats, from)) {
        return obj.chats[from] = {
          messages: []
        };
      }
    };
    obj.logIncoming = function(data) {
      return obj.chats[data.from].messages.push({
        text: data.message,
        type: 'in'
      });
    };
    obj.send = function(chat) {
      return socket.emit('private-message', {
        to: chat.profile_hash,
        from: UserService.profile_hash,
        message: chat.message
      });
    };
    socket.on('private-message', function(data) {
      return obj.logIncoming(data);
    });
    return obj;
  });

  chatModule.directive('chat', function() {
    return {
      restrict: 'E',
      templateUrl: 'scripts/modules/ChatModule/chatModule.html'
    };
  });

  chatModule.controller('ChatCtrl', function($scope, chatModuleService) {
    $scope.send = function(chat) {
      return chatModuleService.send(chat);
    };
    return $scope.chats = chatModuleService.chats;
  });

}).call(this);

If I am not mistaken, when changing the service's value, being assigned to the $scope , it should automatically update the $scope 's variable and therefore update corresponding the HTML. 如果我没记错的话,在将服务的值更改为$scope ,它应该自动更新$scope的变量,并因此更新相应的HTML。 But this doesn't seem to be happening. 但这似乎没有发生。

I solved it myself, the problem was: 我自己解决了,问题是:

obj.chats = [];

is supposed to be: 应该是:

obj.chats = {};

The best is console didn't give me any errors or a clue. 最好的是控制台没有给我任何错误或提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM