Angular JS ng-repeat从数组中选择随机分组?

时间:2016-08-18 18:41:12

标签: javascript angularjs arrays

我有一个包含6个对象的数组。

我有一个带有ng-repeat的列表,我想在这6个项目中显示3个独特的项目。

每次刷新一次,它可能会拉出3个不同的项目,但如果没有,那就没关系,唯一的一点就是3不能相互重复。

例如,如果数组是[red, yellow, blue, green, purple, cyan, fuchsia],那么在刷新时我可以得到:

red,blue,green
purple,blue,yellow
fuchsia,green,red

等。正如你所看到的,我并不在乎蓝色连续两次出现,但我绝不能得到red, blue, blue

我有这段代码:

<ul class="ch-grid">
  <li ng-repeat="user in home.testimonials|orderBy:random|limitTo: 3">
    <div class="ch-item ch-img-1" style="background-image: url(assets/images/{{user.image}})">
      <div class="ch-info">
        <h3>{{user.quote}}</h3>
      </div>
    </div>
    <h3 class="name">{{user.name}}</h3>
    <p class="title">{{user.title}}</p>
  </li>
</ul>

他们在我的控制器中:

_this.random = function () {
  return 0.5 - Math.random();
};

_this.testimonials = [
  {
    name: 'Sara Conklin',
    title: 'SMB/SendPro UX Architect',
    image: 'sara-conklin.jpg',
    quote: 'Instead of inventing original solutions, we can leverage DS guidelines and components, save time, ensure great UX and promote consistency. '},
  {
    name: 'Jenn Church',
    title: 'User Experience Designer',
    image: 'jenn-church.jpg',
    quote: 'The Design System has been a great tool in rapid prototyping, allowing me to get modern, on-brand interfaces put together quickly without having to start from scratch.'},
  {
    name: 'Peter Leeds',
    title: 'Global Creative and Brand Activation',
    image: 'peter-leeds.jpg',
    quote: 'Design System provides the unified, consistent look needed to preserve and reinforce the integrity of our brand.”'},
  {
    name: 'Marcy Goode',
    title: 'Digital Marketing, Self Service &amp; Content Management Leader',
    image: 'marcy-goode.jpg',
    quote: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto, ipsam, mollitia in vitae nemo aliquam.'},
  {
    name: 'Clarence Hempfield',
    title: 'Spectrum Spatial Analyst Product Manager',
    image: 'clarence.jpg',
    quote: 'Design System isn’t just about the interface. It’s about understanding how people are expecting to interact with your technology.'},
  {
    name: 'Aaron Videtto',
    title: 'SendSuite Tracking Online Product Manager',
    image: 'aaron.jpg',
    quote: 'Customers of SendSuite Tracking Online have been up and running within 10-15 minutes. We were able to do this because of the Design System.'}
];

但它并不局限于3,我得到了数十个。

好的,按照@csschapker:

的建议尝试过滤路径
    (function () {
  'use strict';

  angular.module('pb.ds.home').filter('getThree', function () {
    return function (array) {
      var copy = angular.copy(array);
      var sample = [];
      while (sample.length < 3) {
        var randomIndex = Math.floor(Math.random() * (copy.length));
        sample.push(copy[randomIndex]);
      }
      return sample;
    };
  });
})();

    <ul class="ch-grid">
  <li ng-repeat="user in home.testimonials|filter:getThree">
    <div class="ch-item ch-img-1" style="background-image: url(assets/images/{{user.image}})">
      <div class="ch-info">
        <h3>{{user.quote}}</h3>
      </div>
    </div>
    <h3 class="name">{{user.name}}</h3>
    <p class="title">{{user.title}}</p>
  </li>
</ul>

这只是打印出所有6.我必须遗漏一些东西。

3 个答案:

答案 0 :(得分:2)

用户随机和限制过滤器。

<p ng-repeat="i in list|orderBy:random|limitTo:3">{{i}}</p>

答案 1 :(得分:0)

这个答案对于过滤器来说是一个好的开始,但它有问题(参见注释)。我会删除答案,但评论可能对将来的某些人有用。我的新答案是目前解决此问题的更好方法。

您可以使用自定义过滤器:

.filter('randomSample', function() {
    return function(array, length) {
        var copy = angular.copy(array);
        var sample = [];
        while(sample.length < length) {
            var randomIndex = Math.floor(Math.random() * (copy.length));
            sample.push(copy.splice(randomIndex, 1)[0]);
        }
        return sample;
    };
})

使用它像:

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

以下是关于plnkr的示例:http://plnkr.co/edit/NgsQlvgrCD7vLXnBC7q1?p=preview

答案 2 :(得分:0)

经过多次尝试后,在填充数组后,最好在控制器中获取随机值。像这样:

_this.sample = [];
var copy = angular.copy(_this.testimonials);
while(_this.sample.length < 3) {
    var randomIndex = Math.floor(Math.random() * (copy.length));
    _this.sample.push(copy.splice(randomIndex, 1)[0]);
}

然后只是ng-repeat="user in home.sample"