1

My code

<span data-ng-repeat="s in d.sInfo track by $index" title="{{ s.sFirstName }} {{ s.sLastname }}">

<strong>{{ s.sFirstName }} {{ s.sLastname }}</strong> ({{ s.permission }})<br>

</span>

What i want is that if login user and shared user are same then i will show "YOU"

i am trying something like this ,

<strong>{{ s.sFirstName }} {{ s.sLastname }} || s.name == s.FirstName s.Lastname |"YOU"</strong> ({{ s.permission }})

I can do a loop in controller .But is there any way to achieve this in view itself

5 Answers 5

2

Try using the ng-if directive for that

<strong ng-if="!yourConditionHere">{{ s.sFirstName }} {{ s.sLastname }}</strong>
<strong ng-if="yourConditionHere">YOU</strong>

Replace yourConditionHere with your condition.

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

Comments

1

It doesn't look nice (in this case), but it might prove useful in other situation (with more compact expressions): You can use a ternary operator (cond) ? expr : expr

<strong>{{
    ((u.firstname===user.firstname)&&(u.lastname===user.lastname))?
            'YOU':
            u.firstname+' '+u.lastname}}
</strong>

See, also, this short demo.


NOTE

The other approaches mentioned (e.g. using ngShow, ngIf etc) are viable solutions, but they involve the creation/destruction/manipulation of additional DOM elements, which might have a noticable impact on performance if the users list grows big.

BTW, you could also use ngSwitch to the same end.

Comments

1

You can use ng-if or ng-show or ng-hide directives or scope function

Using ng-if directive'

  <strong ng-if="s.FirstName"> {{ s.sFirstName }} {{ s.sLastname }} </strong>
  <strong ng-if="!s.FirstName"> You </strong>

Using ng-show directive'

  <strong ng-show="s.FirstName"> {{ s.sFirstName }} {{ s.sLastname }} </strong>
  <strong ng-show="!s.FirstName"> You </strong>

Using ng-hide directive'

  <strong ng-hide="!s.FirstName"> {{ s.sFirstName }} {{ s.sLastname }} </strong>
  <strong ng-hide="s.FirstName"> You </strong>

Using Scope function

In your case, you can also use scope function to get the name

  <strong> {{getName(s)}} </strong>


  $scope.getName = function (s) {
     if (s.FirstName) return s.FirstName + ' ' + s.LastName;
     return 'You';
  }

Comments

1

ng-if works just fine.

You can also set up a filter that returns the user display name, to avoid putting logic in your markup

I am setting up a jsfiddle to show you an example.

Edit:

There you go

http://jsfiddle.net/jN3Pt/1

angular.filter('userDisplayName', function(loggedInUser) {
  return function(user) {
    if ( loggedInUser.firstName === user.firstName ) return "YOU";
    return user.firstName
  };
});

Comments

0

Perhaps use ng-show or ng-hide ?

<strong ng-show="truthy_condition"></strong>

Comments

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.