角度 1.5 不更新 ng-repeat

Angular 1.5 not update ng-repeat

本文关键字:更新 ng-repeat 角度      更新时间:2023-09-26

>我通过ng-repeat显示一个表格

     <div ng-app="spApp">
<div ng-controller="spListCtrl as MyList">
<table width="100%" cellpadding="10" cellspacing="2">
    <thead>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>CellPhone</th>
        <th>Update</th>
    </thead>
      <tbody>
        <tr ng-repeat="item in MyList.Contacts track by $index">
            <td class="align-center"><input type="text" ng-model="MyList.Contacts[$index].FirstName"> </input></td>
            <td class="align-center">{{MyList.Contacts[$index].Title}}</td>
            <td class="align-center">{{MyList.Contacts[$index].Email}}</td>
            <td class="align-center">{{MyList.Contacts[$index].CellPhone}}</td>
            <td class="align-center"><button ng-click="ShowNewForm(MyList.Contacts[$index])">Изменить</button></td>
        </tr>
    </tbody>
</table>

通过服务加载 ajax 数据

spApp.controller('spListCtrl', function spListCtrl($scope,dataService){
var Contacts;
var promiseObj=dataService.getContacts();
promiseObj.then(function(value) {
Contacts=value; 
 });

我签入调试,数据被分配为正常,但不显示。什么没有尝试过,告诉我我做错了什么。

在使用controllerAs模式时,将数据绑定变量绑定到this(控制器函数上下文),以便您可以使用别名MyList(控制器函数的实例)在HTML上访问它们。

法典

spApp.controller('spListCtrl', function spListCtrl(dataService){
   var self =  this
   var promiseObj=dataService.getContacts();
   promiseObj.then(function(value) {
      self.Contacts=value.data; 
  });
});

在里面ng-repeat使用item来绑定工作。

    <tr ng-repeat="item in MyList.Contacts track by $index">
        <td class="align-center"><input type="text" ng-model="item.FirstName"> </input></td>
        <td class="align-center">{{item.Title}}</td>
        <td class="align-center">{{item.Email}}</td>
        <td class="align-center">{{item.CellPhone}}</td>
        <td class="align-center"><button ng-click="ShowNewForm(item)">Изменить</button></td>
    </tr>