使用AngularJS HTML映射两个集合(内部联接)

时间:2016-06-19 08:04:18

标签: html angularjs json angularjs-scope angularjs-filter

我有两个收藏 user Type 。我希望发送标有 IsPreffered = TRUE 的电子邮件ID,我需要将 email.Type映射到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源代码:

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

在这里,我需要指定类型&#34; 私有&#34;对于上面的HTML。如何将 user[0].Email.Type 映射到 Type.ID 并获取相应的 Type.Name

请使用不在JavaScript级别的HTML级别过滤条件。请帮助我。

2 个答案:

答案 0 :(得分:1)

我建议在代码中而不是标记中进行,但如果需要,可以使用一种方法映射到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>

其中typeCollection为:

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

请注意,您需要在标记中再添加一次ng-repeat,因为过滤器适用于阵列。

编辑: 您也可以使用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>

答案 1 :(得分:0)

for(var i = 0; i < Type.length ; i++)
{ 
   if(user[0].Email.Type to Type[i].ID)
   {
      user[0].Email.TypeName = Type[i].ID;
   }
};

HTML

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