你如何改变ng-repeat的值? (AngularJS)

时间:2015-05-08 12:19:51

标签: mysql angularjs list sorting ng-repeat

我在fiddlethisthisthis SO尝试了此this

我仍然无法让shufflerandom工作。我的代码在这里停了下来:

signupApp.filter('shuffle', function() {
var shuffledArr = [],
  shuffledLength = 0;
return function(arr) {
  if (!arr || !arr.length) { return; }
  var o = arr.slice(0, arr.length);
  if (shuffledLength == arr.length) return shuffledArr;
  for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  shuffledArr = o;
  shuffledLength = o.length;
  return arr.slice(o);
  };
});

我的重复

<ul ng-repeat="codes in response | shuffle">
  <li ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
    <strong>{{codes.id}}</strong> 
    {{codes.code}}
  </li>
</ul>

结果仍然没有随机化我的输出。有人可以帮忙吗?

[更新]:我的代码可以随机化ng-repeat:

<span ng-repeat="codes in response">
  <strong ng-if="((codes.branch == formData.branches.alias) && (codes.taken == 0))">
  {{codes.code}}
  </strong>
</span>

添加limitTo:1后,它不再显示任何内容。在我的控制器中,渔民就像这样运行:

var shuffleArray = function(array) {
    var m = array.length, t, i;

    // While there remain elements to shuffle
    while (m) {
        // Pick a remaining element…
        i = Math.floor(Math.random() * m--);

        // And swap it with the current element.
        t = array[m];
        array[m] = array[i];
        array[i] = t;
    }

    return array;
    }

    http.get("server/fetch.php").success(function(response){
    scope.response = response;
    shuffleArray(scope.response);
  }).error(function() {
    scope.response = "error in fetching data";
  });

我无法弄清楚这有什么问题。当limitTo:设置为6时,它会从列表中输出单个随机值。我也尝试将其设置为10,并从列表中输出两个随机值。我糊涂了。我只想将它限制为1并将其随机化。

0 个答案:

没有答案