1

I have a JSON data which is something like:

$scope.user = {
     is_session_id: true,
     session_id: 'asdasdadssa',
     email     : '[email protected]'
}

Now, I want to assign session_id when 'is_session_by' is true, and email when is_session_by is false\null. The value is to be assigned to id of a div

<div id="{{ user.is_session_id}}  ?  {{  user.session_id }} : {{ user.email  }} " ></div>

I am making some silly mistake. please help

Its coming out like this when checked via "Inspect element":

<li ng-click="selectUser(user,$index)" 
  class="left clearfix ng-scope selected" 
  ng-class="{selected: selected_user.email === user.email}" 
  ng-repeat="user in user_list" 
  id="true  ?  asdasdadssa : [email protected]">

  SOMETHING

 </li>
2
  • 1
    is_session_by is not the same with is_session_id Commented Mar 8, 2017 at 18:58
  • Don't use three mustaches {{ }}. Enclose the entire expression in one set of double curly brackets {{ }}. See AngularJS Developer Guide - Expressions. Commented Mar 8, 2017 at 19:40

5 Answers 5

2

This is the code you need.

<div ng-attr-id="{{ user.is_session_by ? user.session_id : user.email  }}" ></div>
Sign up to request clarification or add additional context in comments.

Comments

2

You could use ng-attr-id which parses your expression :

<div ng-attr-id="{{ user.is_session_id  ?   user.session_id  :  user.email  }} " ></div>

Comments

2

use ng-attr-id t assign id with condition

<div  ng-attr-id="{{ user.is_session_id  ?   user.session_id :  user.email  }} " ></div>

Comments

1

This is simple and recommended for your problem

<div ng-attr-id="{{ user.session_id || user.email  }} " ></div>

No need a extra variable user.isSession_id

Comments

-1

Why you need this way if simple one is there.

In the controller :

 var user = {
         is_session_by: true,
         session_id: 'asdasdadssa',
         email     : '[email protected]'
    }

$scope.result = user.is_session_by  ?   user.session_id  :  user.email 

In the html :

<div id="{{result}}" >{{result}}</div>

5 Comments

I think, there is no need of adding extra property in $scope when ng-attr-id can itself parse the expression
Its not about adding extra property.Its more about code readability,maintainability, testability.
@AkashKC anyway, you evalute it via {{}} ,it will take compilation time and same memory.
comment please.
@RIYAZ : I'd not done downvoting for your post. As comment, I would like to prefer ternary check in html itself rather than adding new property in $scope. As every field defined in the $scope has digest cycle which makes it dirty checking for those property in $scope. I don't want to have any dirty checking for property which can easily be computed as expression in html directive.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.