javascript – AngularJS | orderBy过滤器未动态更新

前端之家收集整理的这篇文章主要介绍了javascript – AngularJS | orderBy过滤器未动态更新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在AngularJS中的orderBy过滤器遇到麻烦.这是我的设置:
<li ng-repeat="item in listItems.data | orderBy:order">
    <a ng-click="getRelated(item._id)">{{ item.title }}</a>
</li>

控制器的一部分:

$scope.order = 'year';

$scope.listItems = $http.post($scope.url,{'filterType': 'abc','letter': $scope.params.letter});

$scope.setOrder = function(order) {
    $scope.order = order;
}

最后我想使用“开关”来订购数据

<span class="sort--title">Sort by</span>
    <a ng-class="{true:'selected',false:''}[order=='title']" href="" ng-click="setOrder('title')" class="sort--attribute">Title</a>
    <a ng-class="{true:'selected',false:''}[order=='year']" href="" ng-click="setOrder('year')" class="sort--attribute">Year</a>
    <a ng-class="{true:'selected',false:''}[order=='length']" href="" ng-click="setOrder('length')" class="sort--attribute">Length</a>
    <a ng-class="{true:'selected',false:''}[order=='date_added']" href="" ng-click="setOrder('date_added')" class="sort--attribute">Date Added</a>

当我点击按钮,列表没有被重新排序.
当我手动更改初始$scope.order值时,该列表按属性排序. ng-class也正确更新.我显然错过了一些东西!

解决方法

我不认为你的想法是错误的.它工作正常这是工作 plunker.

你别在别的地方出错了.

app.js

var app = angular.module('ngApp',[]);

app.controller('MainCtrl',['$scope',function ($scope) {
    'use strict';
    $scope.friends = [
        {name: 'John',phone: '555-1276'},{name: 'Mary',phone: '800-BIG-MARY'},{name: 'Mike',phone: '555-4321'},{name: 'Adam',phone: '555-5678'},{name: 'Julie',phone: '555-8765'}
    ];
    $scope.setOrder = function (order) {
        $scope.order = order;
    };
}]);

主要html

<ul class="nav nav-pills">
    <li ng-class="{'active': order=='name'}"><a href="#" ng-click="setOrder('name')">name</a></li>
    <li  ng-class="{'active': order=='phone'}"><a href="#" ng-click="setOrder('phone')">phone</a></li>
</ul>
<ul>
    <li data-ng-repeat="friend in friends|orderBy:order">
        <span class="name">{{friend.name}}</span>
        <span class="phone">{{friend.phone}}</span>
    </li>
</ul>
原文链接:https://www.f2er.com/js/154480.html

猜你在找的JavaScript相关文章