发表于: 2017-06-15 22:40:14

1 872


今天完成的事情:

最新职位列表完成。


明天的计划:

整理项目,修复bug。


遇到的问题:

1关于ng-repeat的$index的一些问题,删除指定数据错误:

html如下:

<ul ng-controller="ListCtrl">

 <li ng-repeat="item in items">

 {{item.name}}

 <button ng-click="remove($index)">remove</button>

 </li>

</ul>

对应的控制器(controller)如下:

app.controller('ListCtrl', ['$scope', function($scope) {

 $scope.items = getItems();

 

 $scope.remove = function(index) {

 var item = $scope.items[index];

 removeItem(item);

 };

}]);

给列表添加一个过滤器。 允许用户进行搜索。通过 searchFilter 来查询列表中的记录。

<ul ng-controller="ListCtrl">

 <li ng-repeat="item in items | searchFilter">

 {{item.name}}

 <button ng-click="remove($index)">remove</button>

 </li>

</ul>

控制器里面:

$scope.remove = function(index) {

 var item = $scope.items[index];

 removeItem(item);

};

这里使用了 index参数, 然后就遇到了BUG: 过滤后的索引(indexs)不匹配原始列表的索引。

解决方法:不要使用$index,而改成实际的item对象。

<ul ng-controller="ListCtrl">

 <li ng-repeat="item in items | searchFilter">

 {{item.name}}

 <button ng-click="remove(item)">remove</button>

 </li>

</ul>

控制器如下所示:

$scope.remove = function(item) {

 removeItem(item);

};

收获:angular的ng-repeat,ng-change,ng-class理解的更多


返回列表 返回列表
评论

    分享到