TSlegaitis
TSlegaitis

Reputation: 1310

Angular ng-if multiple conditions not working

This is a relatively simple question but I cannot figure out why this isn't working.

So I have ng-if statement in HTML where I check condition if its not true. as in:

<div class="new-asset" data-ng-if="$root.questionData.question_type_id != '1' || $root.questionData.question_type_id != '8'">

But for some reason this does not work. I also tried:

<div class="new-asset" data-ng-if="($root.questionData.question_type_id != '1' || $root.questionData.question_type_id != '8')">

Which has no affect whatsoever... I was reading up and trying to find a solution, someone suggested placing the condition in controller, which would also have performance increase (not 100% sure if it would), as in:

$scope.addNewAssetIf = $root.questionData.question_type_id != '1' || $root.questionData.question_type_id != '8';

and then referencing it in html as:

<div class="new-asset" data-ng-if="addNewAssetIf">

But I cannot use this approach due to asynchronous loading, and dependencies. I need to make this work somehow, I get no errors or anything, even though the question_type_id is 1 I still get .net-asset div shown. If I remove OR statement and only have 1 condition it works:

<div class="new-asset" data-ng-if="$root.questionData.question_type_id != '1' || $root.questionData.question_type_id != '8'">

Any help is appreciated.

Thanks

Upvotes: 1

Views: 3776

Answers (3)

edo.n
edo.n

Reputation: 2420

I'm ignoring everything angular related and trying to understand the basic logic behind the condition. If the ID == 1 won't it satisfy the second condition which asks it to be different from 8? And isn't that true for cases where the ID is 8?

In other words, don't you need && instead of ||?

This should really be a comment but I don't have enough reputation.

Upvotes: 3

nikjohn
nikjohn

Reputation: 21910

Could you try using $rootScope instead of $root and include $rootScope as a dependency in your controller? The issue could just be that the variable is not in scope. You could also assign this to your $scope and then reference it directly in your template

Basically, the idea is to try and see if the values are bound to your template fine. You could even try and

{{$root.questionData.question_type_id}}</div>

To verify this, and then if not, start by addressing the root cause.

Upvotes: 0

ystan-
ystan-

Reputation: 1536

Try using a function:

$scope.addNewAssetIf = function() {
  return $root.questionData.question_type_id != '1' || $root.questionData.question_type_id != '8';
};

Upvotes: 1

Related Questions