我想在七点六下设置7,如何在不更改$ scope.myJson的情况下将其固定为可见。

<div ng-app='app' ng-controller='mainCtrl'>
    {{ctrlTest}}<hr/>Ng-Repeat<hr/>
    <div ng-repeat="json in myJson">
        <li>{{json}}</li>
    </div>
</div>

angular.module('app',['QuickList']).controller('mainCtrl', function($scope){
    $scope.ctrlTest = "Applying";
    $scope.myJson = ["one", "two", "three", "four", "five", "six", "eight", "nine", "seven"]
    $scope.jsons = function(){
        console.log('callback called');
    }
})


请看小提琴
http://jsfiddle.net/HgDA7/757/

最佳答案

以下是我通过创建自定义过滤器和自定义compareFunction想到的唯一解决方案



angular.module('app', []).controller('mainCtrl', function($scope) {
  $scope.ctrlTest = "Applying";
  $scope.myJson = ["one", "two", "three", "four", "five", "six", "eight", "nine", "seven"]
  $scope.jsons = function() {
    console.log('callback called');
  }
}).filter('customsort', function() {
  return function(list) {
    var sortOrder = {
     'one': 1,
     'two': 2,
     'three': 3,
     'four': 4,
     'five': 5,
     'six': 6,
     'seven': 7,
     'eight': 8,
     'nine': 9
    }
    list.sort(function(a, b) {
      return sortOrder[a] - sortOrder[b];
    })
    return list;
  }
})

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app' ng-controller='mainCtrl'>
  {{ctrlTest}}
  <hr/>Ng-Repeat
  <hr/>
  <div ng-repeat="json in myJson | customsort">
    <li>{{json}}</li>
  </div>
</div>

关于javascript - 交换angularjs中的ng-repeat元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47902381/

10-09 17:29