angularjs 带过滤器的高级 ng-options
angularjs advanced ng-options with filter
我有这个 json 对象:
{
"title":"Training Pants",
"modifiers":[
{
"id":"1172499777372291315",
"type":"variant",
"title":"Gender",
"variants":[
{
"id":"1172499862214672628",
"modifierId":"1172499777372291315",
"title":"Male"
},
{
"id":"1172499953986044150",
"modifierId":"1172499777372291315",
"title":"Female"
},
{
"id":"1172500083497763064",
"modifierId":"1172499777372291315",
"title":"Junior"
}
]
},
{
"id":"1172500938313695482",
"type":"single",
"title":"Cuffs",
"variants":[
{
"id":"1172501102411645179",
"modifierId":"1172500938313695482",
"title":"Elasticated"
},
{
"id":"1172501247098355967",
"modifierId":"1172500938313695482",
"title":"Combination"
},
{
"id":"1172501400207229187",
"modifierId":"1172500938313695482",
"title":"Open End"
}
]
},
{
"id":"1172501710124351751",
"type":"single",
"title":"Pockets",
"variants":[
{
"id":"1172501859257024776",
"modifierId":"1172501710124351751",
"title":"Open Side"
},
{
"id":"1172502043185643794",
"modifierId":"1172501710124351751",
"title":"Zipped Side"
}
]
},
{
"id":"1172502381967966492",
"type":"single",
"title":"Accessories",
"variants":[
{
"id":"1172502597169316125",
"modifierId":"1172502381967966492",
"title":"Waist Cord"
},
{
"id":"1172502756494147870",
"modifierId":"1172502381967966492",
"title":"No.5 Chunky Zip (12\" Leg)"
},
{
"id":"1172502919115702559",
"modifierId":"1172502381967966492",
"title":"No.5 Chunky Zip (18\" Leg)"
}
]
}
]
}
如您所见,它有一个 修饰符数组 。我需要按类型过滤掉 modifers,并且只需要 select variant。
我这样写我的 ng-repeat:
ng-options="gender.title as gender.title for gender in filters = (item.garment.modifiers | filter: { type: 'variant' })"
但这不是我想要的。我真正想要的是 modifier 中的 variants。我希望我能做到这一点:
ng-options="gender.title as gender.title for gender in filters.variants = (item.garment.modifiers | filter: { type: 'variant' })"
但这没有用。
有没有一种简单的方法可以做到这一点,还是我必须创建一个函数?
我认为这应该可行:
ng-options="gender.title as gender.title for gender in filters = (item.garment.modifiers | filter: { type: 'variant' }).variants"
我有这个 json 对象:
{
"title":"Training Pants",
"modifiers":[
{
"id":"1172499777372291315",
"type":"variant",
"title":"Gender",
"variants":[
{
"id":"1172499862214672628",
"modifierId":"1172499777372291315",
"title":"Male"
},
{
"id":"1172499953986044150",
"modifierId":"1172499777372291315",
"title":"Female"
},
{
"id":"1172500083497763064",
"modifierId":"1172499777372291315",
"title":"Junior"
}
]
},
{
"id":"1172500938313695482",
"type":"single",
"title":"Cuffs",
"variants":[
{
"id":"1172501102411645179",
"modifierId":"1172500938313695482",
"title":"Elasticated"
},
{
"id":"1172501247098355967",
"modifierId":"1172500938313695482",
"title":"Combination"
},
{
"id":"1172501400207229187",
"modifierId":"1172500938313695482",
"title":"Open End"
}
]
},
{
"id":"1172501710124351751",
"type":"single",
"title":"Pockets",
"variants":[
{
"id":"1172501859257024776",
"modifierId":"1172501710124351751",
"title":"Open Side"
},
{
"id":"1172502043185643794",
"modifierId":"1172501710124351751",
"title":"Zipped Side"
}
]
},
{
"id":"1172502381967966492",
"type":"single",
"title":"Accessories",
"variants":[
{
"id":"1172502597169316125",
"modifierId":"1172502381967966492",
"title":"Waist Cord"
},
{
"id":"1172502756494147870",
"modifierId":"1172502381967966492",
"title":"No.5 Chunky Zip (12\" Leg)"
},
{
"id":"1172502919115702559",
"modifierId":"1172502381967966492",
"title":"No.5 Chunky Zip (18\" Leg)"
}
]
}
]
}
如您所见,它有一个 修饰符数组 。我需要按类型过滤掉 modifers,并且只需要 select variant。 我这样写我的 ng-repeat:
ng-options="gender.title as gender.title for gender in filters = (item.garment.modifiers | filter: { type: 'variant' })"
但这不是我想要的。我真正想要的是 modifier 中的 variants。我希望我能做到这一点:
ng-options="gender.title as gender.title for gender in filters.variants = (item.garment.modifiers | filter: { type: 'variant' })"
但这没有用。 有没有一种简单的方法可以做到这一点,还是我必须创建一个函数?
我认为这应该可行:
ng-options="gender.title as gender.title for gender in filters = (item.garment.modifiers | filter: { type: 'variant' }).variants"