使用 AngularJS HTML 从 Collection 过滤嵌入的 Json 元素
Filter Embedded Json element from a Collection using AngularJS HTML
我有一个 JSON Collection,我希望使用 [=42] 显示标记为 IsPreffered = TRUE
的电子邮件 ID =] HTML 没有。
user.Name
我的 JSON Collection :
{ "user" : [
{
"Name" : "B. Balamanigandan",
"Email": [
{
"Id": "bala@gmail.com",
"IsPreffered": true
},
{
"Id": "mani@gmail.com",
"IsPreffered": false
}
]}
]};
HTML源代码:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span> {{collection.user.Email.Id}} </span>
</div>
Collectionuser
只包含一个文档。所以,我没有使用 ng-repeat。 user
是 Collection 而不是文档 。我还需要一张支票。如果不止一封电子邮件有“IsPreferred == true”,那么我会选择最后一封。请帮助我。
请使用HTML级别不在JavaScript级别过滤条件。请帮助我。
而不是这个:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span> {{collection.user.Email.Id}} </span>
</div>
你必须这样做:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user[0].Name}} </span>
<span ng-repeat="email in collection.user[0].email| filter: {IsPreffered : 'true'}"> {{email.Id}} </span>
</div>
您必须使用数组语法,因为虽然只有 1 个用户对象,但它在 json 中被构造为一个数组(请注意 [ ],电子邮件也是如此)。
这是一个fiddle:http://jsfiddle.net/Lvc0u55v/5593/
更新:要仅显示最后一封真实的电子邮件,请使用 ng-if:
<span ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} " ng-if="$last"> {{email.Id}}</span>
这是更新后的 fiddle:
http://jsfiddle.net/Lvc0u55v/5594/
一种方法是在用户电子邮件数组上使用 ngRepeat
和 filter 以仅获取 "IsPreffered": true
中的那些电子邮件。过滤器将 Emails 数组中的每个对象与过滤器对象 (filterEmail
) 进行比较。此过滤器对象在控制器中定义 - 这样您就可以按需更改甚至动态设置它。
var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.collection = {
"user": {
"Name": "B. Balamanigandan",
"Email": [{
"Id": "bala@gmail.com",
"IsPreffered": true
}, {
"Id": "mani@gmail.com",
"IsPreffered": false
}]
}
};
$scope.filterEmail = {
"IsPreffered": true
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span ng-repeat="email in collection.user.Email | filter : filterEmail ">
{{email.Id}}
</span>
</div>
</body>
注意:据我所知,将 user
作为对象内部的数组没有多大意义(在我的示例中,我删除了内部数组)。如果您坚持保留此数据模式,则需要将 collection.user
的每个引用更改为 collection.user[0]
。
我有一个 JSON Collection,我希望使用 [=42] 显示标记为 IsPreffered = TRUE
的电子邮件 ID =] HTML 没有。
user.Name
我的 JSON Collection :
{ "user" : [
{
"Name" : "B. Balamanigandan",
"Email": [
{
"Id": "bala@gmail.com",
"IsPreffered": true
},
{
"Id": "mani@gmail.com",
"IsPreffered": false
}
]}
]};
HTML源代码:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span> {{collection.user.Email.Id}} </span>
</div>
Collectionuser
只包含一个文档。所以,我没有使用 ng-repeat。 user
是 Collection 而不是文档 。我还需要一张支票。如果不止一封电子邮件有“IsPreferred == true”,那么我会选择最后一封。请帮助我。
请使用HTML级别不在JavaScript级别过滤条件。请帮助我。
而不是这个:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span> {{collection.user.Email.Id}} </span>
</div>
你必须这样做:
<div>
<h3>Employee Details:</h3>
<span> {{collection.user[0].Name}} </span>
<span ng-repeat="email in collection.user[0].email| filter: {IsPreffered : 'true'}"> {{email.Id}} </span>
</div>
您必须使用数组语法,因为虽然只有 1 个用户对象,但它在 json 中被构造为一个数组(请注意 [ ],电子邮件也是如此)。
这是一个fiddle:http://jsfiddle.net/Lvc0u55v/5593/
更新:要仅显示最后一封真实的电子邮件,请使用 ng-if:
<span ng-repeat="email in collection.user[0].Email| filter: {IsPreffered : true} " ng-if="$last"> {{email.Id}}</span>
这是更新后的 fiddle: http://jsfiddle.net/Lvc0u55v/5594/
一种方法是在用户电子邮件数组上使用 ngRepeat
和 filter 以仅获取 "IsPreffered": true
中的那些电子邮件。过滤器将 Emails 数组中的每个对象与过滤器对象 (filterEmail
) 进行比较。此过滤器对象在控制器中定义 - 这样您就可以按需更改甚至动态设置它。
var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
$scope.collection = {
"user": {
"Name": "B. Balamanigandan",
"Email": [{
"Id": "bala@gmail.com",
"IsPreffered": true
}, {
"Id": "mani@gmail.com",
"IsPreffered": false
}]
}
};
$scope.filterEmail = {
"IsPreffered": true
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app" ng-controller="ctrl">
<div>
<h3>Employee Details:</h3>
<span> {{collection.user.Name}} </span>
<span ng-repeat="email in collection.user.Email | filter : filterEmail ">
{{email.Id}}
</span>
</div>
</body>
注意:据我所知,将 user
作为对象内部的数组没有多大意义(在我的示例中,我删除了内部数组)。如果您坚持保留此数据模式,则需要将 collection.user
的每个引用更改为 collection.user[0]
。