0

how can I add extra condition on below code

<div ng-class="{true: 'complete'}[item.Id != 0]"></div>

here I need to add "{true: 'abc'}[item.name == "FName"], can anyone help me out.

thank you

4
  • what are you trying to achieve? what is that condition suppose to mean? Commented Sep 16, 2019 at 8:25
  • i just wanted to add extra condition. Commented Sep 16, 2019 at 8:27
  • true: 'abc' is not a condition... ng-class=" (item.id != 0 && /* some other condition*/) ? 'classNameWhenConditionsMet' : 'classNameWhenConditionsAreNotMet'" is what you are looking for ? Commented Sep 16, 2019 at 8:33
  • I need to add two different classes based on the condtions Commented Sep 16, 2019 at 9:59

2 Answers 2

2

After doing a quick search i found this:

ng-class="{'test': obj.value1 == 'someothervalue' || obj.value2 == 'somethingelse'}"

Source: AngularJS ngClass conditional Credits go to https://stackoverflow.com/users/736482/bertrand

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

4 Comments

I need to add two different classes based on the condtions
So you need something like if condition : class elseif condition:class else nothing ?
No, both condtion should work, as shown below <div ng-class="{true: 'complete'}[item.Id != 0],{true: 'abc'}[item.name == "FName"]"></div> here abc is class, but here second condition is not working
I have no ideea of your conditions but what I have noticed is that you double quoted Fname which will break your code: <div ng-class="{true: 'complete'}[item.Id != 0],{true: 'abc'}[item.name == 'FName']"></div>
1

I guess that you want complete class to be added with this condition: item.Id != 0. The correct syntax for this is the below:

<div ng-class="{ 'complete': item.Id != 0 }"></div>

You can add as many classes you want using a comma:

<div ng-class="{ 'complete': item.Id != 0, 'abc': item.name == 'FName' }"></div>

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.