0

I'm having a JSON Collection, I wish to dispay Email ID which is marked as IsPreffered = TRUE using AngularJS HTML without . user.Name

My JSON Collection :

{ "user" : [
    {
        "Name" : "B. Balamanigandan",
        "Email": [
            {
                "Id": "[email protected]",
                "IsPreffered": true
            },
            {
                "Id": "[email protected]",
                "IsPreffered": false
            }
    ]}
]};

HTML Source Code:

<div>
    <h3>Employee Details:</h3>
    <span> {{collection.user.Name}} </span>
    <span> {{collection.user.Email.Id}} </span>
</div>

The Collection user contains only one document. So, I didn't use ng-repeat. user is a Collection not a Document. One more check I need. If more than one email has `IsPreferred == true', then I have the take the last one. Kindly assist me.

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

2
  • Where is your ngRepeat? Commented Jun 19, 2016 at 6:31
  • @dfsq The Collection user contains only one document. So, I didn't use ng-repeat. user is a Collection not a Document. Kindly assist me. Commented Jun 19, 2016 at 6:34

2 Answers 2

1

instead of this:

<div>
    <h3>Employee Details:</h3>
    <span> {{collection.user.Name}} </span>
    <span> {{collection.user.Email.Id}} </span>
</div>

you will have to do this:

<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>

You have to use array syntax, because although there is only 1 user object, it is structured in the json as an array ( note the [ ] , same for Email).

Here is a fiddle: http://jsfiddle.net/Lvc0u55v/5593/

UPDATE: For showing only the last email which is true, use ng-if:

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

Here is the updated fiddle: http://jsfiddle.net/Lvc0u55v/5594/

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but one more check I need. If more than one email has `IsPreferred == true', then I have the take the last one. Kindly assist me please.
Please update your question details, to mention this.
I need one more solution from you. I posted a new question stackoverflow.com/questions/37905191/… Kindly assist me.
0

One way to do this is to use ngRepeat on the user Emails array with a filter to get only those Emails where "IsPreffered": true. The filter compares each object in the Emails array with the filter object (filterEmail). This filter object is defined in the controller - this way you will be able to change by demand or even set it dynamically.

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  $scope.collection = {
    "user": {
      "Name": "B. Balamanigandan",
      "Email": [{
        "Id": "[email protected]",
        "IsPreffered": true
      }, {
        "Id": "[email protected]",
        "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>

NOTE: To my understanding it doesn't make much sense to have user as an array inside an object (in my example I removed the inner array). If you insist on keeping this data-schema, you will need to change every reference of collection.user to collection.user[0].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.