与AngularJS过滤器过滤数据。如何通过迭代的对象? [英] Filtering data with AngularJS Filter. How to iterate over objects?

查看:82
本文介绍了与AngularJS过滤器过滤数据。如何通过迭代的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的过滤器:

  app.filter('ticketsFilter',函数(){
返回功能(输入){
    VAR数据=输入;
    的console.log(过滤的对象:)
    的console.log(数据);
        返回输入;
};
});

这是我的标记:

 < TR NG重复=票中票| ticketsFilter>
  &所述p为H.; {{ticket.createdByName}}&下; / P>
  &所述p为H.; {{ticket.title}}&下; / P>
  &所述p为H.; {{ticket.toDpt}}&下; / P>
< / TR>

门票对象是一个对象的对象。在我的数据库,有票的4个实例。而且,当我的票打过滤,过滤的对象显示为:

  [对象,对象,目标,对象]

我无法弄清楚如何接取这些对象。我试着使用$指数,获得了依赖注入。试图用一个(在输入x)的循环,但在执行console.log将只显示最后一个对象的属性。我试过了(我,我

我可以接取正确使用此我对象的属性:

 的console.log(数据[0]属性约);

但我不知道如何遍历和接取数据[I],而不是数据[0]。好了,关于如何接取我的对象对象的索引任何提示?如果需要的话我可以给更多的code。

感谢您的任何建议。

@EDIT

这是我的console.log($ scope.tickets)的样子:

  fromDpt:数组[4]
0:对象
1:对象
2:对象
3:对象

每一个物体看起来是这样的:

  0:对象
__v:0
_id:55ddb1e6ca11301020ef9b77
createdAt:1440592358061
createdById:55d758ee3a7ff720141a18f8
__proto__:对象


解决方案

所有的你给我说说这个问题很大的启示。我感谢你们每一个和你们每个人。由于没有有一个完整的答案,我会总结所有的技巧和创建完整的回答这个问题:

首先。当我试图遍历

 < TR NG重复=票中票| ticketsFilter>

在ticketsFilter将获得门票的实例。门票是包含多个售票对象的对象,并有一些特性每张票。所以:

  $ scope.tickets = {
  门票:{名称:'菲利普',状态:好},
  门票:{名称:'R2B21',状态:可悲的},
  门票:{名称:'42',状态:好},
  门票:{名称:'明合文吉,状态:'坏'}
};

知道了我的过滤器将收门票作为输入,我可以在它的内部有一个正确的到达每个票循环。通过这种方式:

  app.filter('ticketsFilter',函数(){
  返回功能(门票){
    对于(VAR I = 0; I< tickets.length;我++){...

在for循环的,我会达到包含在门票每票。所以,我只需要抓住他们,并检查他们是否有我想要的propertie。我这种情况下,我想回到只有那些好的状态门票。所以:

  VAR票票= [I] //掠门票里面的for循环。
如果(ticket.status =='好'){//检查是否我的机票是好的。

现在,我需要两件事情。首先,我要回我的数据,我需要每个过滤好票推到我返回的数据。我需要记住,因子评分,返回的对象必须不为1票,但票。正因为如此,我将创建一个名为空数组前过滤循环。而且,推用我,如果插入票[I]有身份地位的propertie =='好'。

所以,完整的code将是:

  app.filter('ticketsFilter',函数(){
返回功能(门票){
    //创建空的对象,将返回数据
    VAR数据= [];
    对于(VAR I = 0; I< tickets.length;我++){
        VAR票票= [I]
        如果(ticket.status =='好'){
            //插入好数据的内部票
            data.push(票);
        };
    };
    //最后,返回数据
    返回的数据;
};
});

之后,如果我有标记:

 < TR NG重复=票中票| ticketsFilter>
  < P>< B> {{ticket.name}}&下; / B个;&下; / P>
< / TR>

这将显示:

飞利浦

42

,就是这样,遍历对象的数组,并检查其属性。我要感谢,再次,每一个试图帮助,并希望抱歉我不好解释。

感谢您!

This is my Filter:

app.filter('ticketsFilter', function (){
return function (input){
    var data = input;
    console.log("Filtered object:")
    console.log(data);
        return input;
};
});

This is my Markup:

<tr ng-repeat="ticket in tickets | ticketsFilter">
  <p> {{ticket.createdByName}} </p>
  <p> {{ticket.title}} </p>
  <p> {{ticket.toDpt}} </p>
</tr> 

The tickets Object is a Object of Objects. On my DB, there are 4 instances of tickets. And, when my ticket hits the filter, the Filtered Object is displayed as:

[Object, Object, Object, Object]

I could not figure out how to acess these objects. I've tried to use $index, received a dependency injection. Tried to use a (for x in input) loop, but the console.log will only display the last object properties. I've tried for(i, i

I can properly acess my objects properties using this:

console.log(data[0].attribute); 

But I dont know how to iterate and acess data[i] instead of data[0]. Well, any tips on how to acess my Object Objects index? I can give more code if needed.

Thanks for any advice.

@EDIT

This is how my console.log($scope.tickets) looks like:

fromDpt: Array[4]
0: Object
1: Object
2: Object
3: Object

Every Object looks like this:

0: Object
__v: 0
_id: "55ddb1e6ca11301020ef9b77"
createdAt: "1440592358061"
createdById: "55d758ee3a7ff720141a18f8"
__proto__: Object

解决方案

All of you gave me great insight about this problem. I thank you every and each of you. Since none had a full answer, I'm going to summarize all the tips and create a full answer to this problem:

First of all. When I'm trying to iterate over

<tr ng-repeat="ticket in tickets | ticketsFilter">

the ticketsFilter will receive a instance of ticketS. TicketS is an Object containing multiple ticket Objects, and each ticket having some properties. So:

$scope.tickets = {
  ticket: { name: 'Philip', status: 'good' },
  ticket: { name: 'R2B21', status: 'sad' },
  ticket: { name: '42', status: 'good' },
  ticket: { name: 'MVW', status: 'bad' }
};

Knowing that my filter will receive tickets as input, I could reach every ticket inside of it with a proper for loop. This way:

app.filter('ticketsFilter', function () { 
  return function(tickets){
    for (var i = 0; i < tickets.length; i ++) { ...

Inside of the for loop, I will reach each and every ticket contained in tickets. So, I just need to grab them and check if they have the propertie that I want. I this case, I want to return only tickets that have a status of 'good'. So:

var ticket = tickets[i]; //Grabing the ticket inside the for loop.
if (ticket.status == 'good') { //Checking if my ticket is good.

Now, I need two things. First, I need to return my data and I need to push every filtered 'good' ticket into my returned data. I need to remember, tought, that the returned object must not be 1 ticket, but ticketS. Because of that, I'm going to create a empty array called filtered before the for loop. And, to push use my if to insert ticket[i] that have a propertie of status=='good'.

So, the full code will be:

app.filter('ticketsFilter', function (){
return function (tickets){
    //creating the empty object that will return data
    var data = []; 
    for (var i = 0; i < tickets.length; i ++) {
        var ticket = tickets[i];
        if (ticket.status == 'good'){
            //Inserting 'good' ticket inside of data
            data.push(ticket);
        };
    };
    //And, finally, returning the data
    return data;
};
});

After that, If I have the Markup:

<tr ng-repeat="ticket in tickets | ticketsFilter">
  <p><b> {{ticket.name}} </b></p>
</tr> 

It would display:

Philip

42

And that is it, iterating over an array of objects and checking their properties. I want to thank, again, every one that tried to help and want to sorry about my bad explanation.

Thank you!

这篇关于与AngularJS过滤器过滤数据。如何通过迭代的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆