Angular orderBy不排序字符串数组

时间:2015-08-02 20:01:35

标签: javascript angularjs

我正在尝试对数据进行排序,但这些值没有排序,我不确定原因。

这是在Modal的模板中:

<input type="text" placeholder="Filter items" class="form-control" ng-model="searchText">

<ul>
    <li ng-repeat="item in items | filter:searchText | orderBy">
        <a href="#" ng-click="selected.item = item">{{item}}</a>
    </li>
</ul>

我过滤的数组看起来像这样:

{
    "actions": [
        "Views",
        "Clicks",
        "Uploads",
        "Downloads"
    ]
}

当它构建列表时,项目的顺序不会改变,因此我认为我对orderBy做错了。我也像其他人所建议的那样尝试orderBy:'toString()',但这也不起作用。

以下是我正在使用的JavaScript(我不确定这是否重要),我正在使用Angular Bootstrap Modal

app.directive('events', function($modal, $http){
    return{
        restrict: 'AE',
        templateUrl: '/templates/dashboard/widgets/event.html',
        scope: {
            source: '@'
        },
        link: function($scope){
            $scope.items = [];

            $http({
                url: $scope.source
            }).success(function(data){
                $scope.items = data;
            });

            $scope.open = function(modalData){
                var modalInstance = $modal.open({
                    animation: true,
                    controller: 'eventOptions',
                    templateUrl: '/templates/dashboard/modals/event.html',
                    resolve: {
                        items: function(){
                            return $scope.items;
                        },
                        modalData: function(){
                            return modalData;
                        }
                    }
                });
            };
        }
    };
});

app.controller('eventOptions', function($scope, $modalInstance, items, modalData){

    $scope.items = items[modalData];

    $scope.selected = {
        item: $scope.items[0]
    };

    $scope.ok = function(){
        $modalInstance.close($scope.selected.item);
    };

    $scope.cancel = function(){
        $modalInstance.dismiss('cancel');
    };
});

1 个答案:

答案 0 :(得分:0)

我偶然想通了(通过做一些家务),我只需要改变我的决心:

resolve: {
    items: function(){
        return $scope.items[modalData];
    }
}

然后我在控制器中更改了这个:

$scope.items = items[modalData];

太过于:

$scope.items = items;