使用AngularJS HTML映射两个集合(Inner Join) [英] Map two Collections (Inner Join) using AngularJS HTML

查看:161
本文介绍了使用AngularJS HTML映射两个集合(Inner Join)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Collection user Type 。我希望显示标记为 IsPreffered = TRUE 的电子邮件ID,并且需要将 email.Type映射到Type.ID

  {user:[
{
Name:B. Balamanigandan ,
电子邮件:[
{
Id:bala@gmail.com,
IsPreffered:true,
Type: 1,
},
{
Id:mani@gmail.com,
IsPreffered:false,
Type:2,
}
]}
]};

{
类型:[
{
ID:1,
姓名:私人
},
{
ID:2,
姓名:主页
},
{
ID:3,
Name:Business
},
]}

HTML源代码:

 < span ng-repeat =email in collection.user [0] .Email | filter:{IsPreffered :true}ng-if =$ last> {{email.Id}}< /跨度> 

这里我需要为上述HTML指定类型私人 。如何将 用户[0] .Email.Type 映射到 Type.ID ,然后获取相应的 Type.Name 使用HTML级别的条件不在JavaScript级别。请帮助我。

解决方案

我会建议用代码而不是标记来做,但如果您需要这样做,可以映射到类型:

 < span 
ng-repeat =email in collection.user [0]。电子邮件|过滤器:{IsPreffered:true}
ng-if =$ last>
电子邮件地址:{{email.Id}},
< span ng-repeat =typeCollection.Type中的类型名称|过滤器:{ID:email.Type}>
类型:{{typename.Name}}
< / span>
< / span>

其中typeCollection是:

  $ scope.typeCollection = {
Type:[{
ID:1,
Name:Private
}, {
ID:2,
姓名:家庭
},{
ID:3,
姓名:
},]
};

请注意,您需要在标记中再添加一个ng-repeat,因为过滤器对数组起作用。 p>

编辑:
您也可以使用ng-init来执行此操作:

  ng-repeat =email in collection.user [0] .Email | filter:{IsPreffered:true}
ng-if =$ last>
电子邮件:{{email.Id}},
< span ng-init =types =(typeCollection.Type | filter:{ID:email.Type})>
类型:{{types [0] .Name}}
< / span>
< / span>


I'm having two Collection user and Type. I wish to dispay Email ID which is marked as IsPreffered = TRUE and I need to map the email.Type to Type.ID

{ "user" : [
    {
        "Name" : "B. Balamanigandan",
        "Email": [
            {
                "Id": "bala@gmail.com",
                "IsPreffered": true,
                "Type": 1,
            },
            {
                "Id": "mani@gmail.com",
                "IsPreffered": false,
                "Type": 2,
            }
    ]}
]};

{
    "Type": [
        {
            "ID": 1,
            "Name": "Private"           
        },
        {
            "ID": 2,
            "Name": "Home"           
        },
        {
            "ID": 3,
            "Name": "Business"           
        },
]}

HTML Source Code:

<span ng-repeat="email in collection.user[0].Email | filter: {IsPreffered : true} " ng-if="$last"> {{email.Id}}</span>

Here I need to Specify the Type "Private" for the above HTML. How to Map the user[0].Email.Type to Type.ID and fetch the appropriate Type.Name

Kindly filter the condition using HTML Level not in JavaScript Level. Kindly assist me.

解决方案

I would suggest doing it in code rather than the markup, but if you need this is one way you can map to Type:

<span 
ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} " 
ng-if="$last"> 
    Email : {{email.Id}} ,
    <span ng-repeat="typename in typeCollection.Type|filter:{ID : email.Type}">
        Type: {{typename.Name}}
    </span>
</span>

where typeCollection is :

$scope.typeCollection = {
    "Type": [{
      "ID": 1,
      "Name": "Private"
    }, {
      "ID": 2,
      "Name": "Home"
    }, {
      "ID": 3,
      "Name": "Business"
    }, ]
  };

Please note that you needed one more ng-repeat in markup because filter works on an array.

EDIT: You can also do this using ng-init:

<span 
ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} " 
ng-if="$last"> 
    Email : {{email.Id}} ,
    <span ng-init="types = (typeCollection.Type|filter:{ID : email.Type})">
        Type: {{types[0].Name}} 
    </span>
</span>

这篇关于使用AngularJS HTML映射两个集合(Inner Join)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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