随机重复结果

时间:2017-04-25 12:23:14

标签: javascript angularjs firebase-realtime-database angularjs-ng-repeat angularfire

我在Firebase中有一些数据,我将它放在我的网站上并过滤它们。我想只显示4个随机物品,其属性为“cheap:true”。不幸的是,我不知道如何改变这个输出。我创建了随机函数,但它会导致错误:

[$rootScope:infdig]

如果你能帮助我改变这4个结果,我将非常感激。这是我的代码:

HTML:

<div ng-repeat="place in places | filter: {cheap: 'true'} | limitTo: 4 | orderBy: random" class="col-lg-3">

JS:

// Random order by
    $scope.random = function() {
      return 0.5 - Math.random();  
    };

我的Firebase数据在这里:

// Firebase
    $scope.places = $firebaseArray(ref);

3 个答案:

答案 0 :(得分:0)

您可以让过滤器返回随机拼接。

<li ng-repeat="item in array | randomSample:4">{{ item }}</li>

尝试此解决方案 - http://plnkr.co/edit/NgsQlvgrCD7vLXnBC7q1?p=preview

(发布于此问题Angular JS ng-repeat choose random grouping from array?

答案 1 :(得分:0)

试试这个

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            requestPermissions(new String[]{Manifest.permission.CAMERA}, 2);
            requestPermissions(new String[]{Manifest.permission.READ_SMS}, 3);


        }

答案 2 :(得分:0)

您可以尝试如下。请参阅AngularJS ng-repeat random order

&#13;
&#13;
var app=angular.module("myapp", []);
		app.controller("namesctrl", function($scope,$http,$window){
 
        $scope.tickets = [{
                "id": 1
            },{
                "id": 2
            },{
                "id": 3
            },{
                "id": 4
            },{
                "id": 5
            },{
                "id": 6
            },{
                "id": 7
            }];
     
      function sort(array) {
        return array.sort(function() {
            return .5 - Math.random();
        });
      }

      function random() {
        for (var key in $scope.tickets) {
          $scope.tickets = sort($scope.tickets);
        }
      }      
      random();

		});
&#13;
	<script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.3/angular.js"></script>
<body ng-app="myapp" ng-controller="namesctrl">
    <ul>
        <li ng-repeat="item in tickets">
        {{item.id}} {{item}}
        </li>
    </ul>
</body>
&#13;
&#13;
&#13;