ng-repeat不起作用。认为存在范围问题,但不确定。

时间:2016-03-08 00:12:31

标签: angularjs angularjs-directive angularjs-scope

所以我有以下div:

<div id="chatrooms" ng-controller="TableMsgController">
<section id="chatroom">
<div id="messages"  >
    <ul>
      <li ng-repeat="entry in seenMsgs">{{entry.msg}}</li>
    </ul>
 </div>
 <form action="">
     <input type="text" id="userMsg" ng-model="userMsg">
    <input type="button" class="gButton" id= "sendMsgBtn" value="Send" ng-click="sendMsg()" />

    </form>
  </section>
</div>

我有以下控制器:

tables.controller('TableMsgController',function ($rootScope,$scope) {
  $scope.msgSeen = [];
  $scope.sendMsg = function () {
    //console.log("sendMsg button pushed");
    //console.log($scope.userMsg);
    $scope.msgSeen.push( {'msg':$scope.userMsg} );
   // console.log($scope.msgSeen);
};

ng-repeat不起作用。

知道为什么吗?

ž

1 个答案:

答案 0 :(得分:2)

我可以发现两件事:

a)你错过sendMsg上的右大括号:

tables.controller('TableMsgController',function ($rootScope,$scope) {
  $scope.msgSeen = [];
  $scope.sendMsg = function () {
    //console.log("sendMsg button pushed");
    //console.log($scope.userMsg);
    $scope.msgSeen.push( {'msg':$scope.userMsg} );
   // console.log($scope.msgSeen);
  }    <---------------------------------------------- :)
};

和b)您在范围中定义msgSeen但是正在尝试迭代seenMsgs

<li ng-repeat="entry in seenMsgs">{{entry.msg}}</li>

解决这两个问题,看看是否能解决问题。