'ng-if' - Multiple condition not working on template - angularjs

I have 3 variety of template. by $index conditionally i am applying the template.
here is my directive html :
<all-apps-gallery index="$index" app="app" update="update(app)" class="show" ng-repeat="app in allAppsBatch"></all-apps-gallery>
and here is the directive:
var allAppsGallery = function ($compile, $animate) {
return {
restrict : 'E',
replace : true,
templateUrl : '/views/tools/gallery.html', // see below the html
scope : {
index : "=",
update : "&",
app : "="
}
}
}
}
angular
.module("tcpApp")
.directive('allAppsGallery', allAppsGallery);
My `templateUrl''s html :
<div>
<div ng-class="bgr box{{index}}" ng-click="update(app)" ng-if="{{index}} == 0 || {{index}} == 13 || {{index}} == 14 || {{index}} == 15 || {{index}} == 5 ">
<h2>{{app.completion}} % {{index}}</h2>
<span>{{app.name}}</span>
</div>
<div ng-class="bbr box{{index}}" ng-click="update(app)" ng-if="{{index}} == 2 || {{index}} == 3 || {{index}} == 4 || {{index}} == 6 || {{index}} == 10 || {{index}} == 11 || {{index}} == 12">
<h2>{{app.completion}} %</h2>
<span>{{app.name}}</span>
</div>
<div ng-class="bbl box{{index}}" ng-click="update(app)" ng-if="{{index}} == 1 || {{index}} == 7 || {{index}} == 8 || {{index}} == 9 ">
<h2>{{app.completion}} %</h2>
<span>{{app.name}}</span>
</div>
</div>
Here according to the index value, i am switching the template. but it's not working for me. what is the wrong i made here?
Any one figure-out me please?
Thanks in advance.
I am getting this error:
Error Link

ngIf directive expects an expression, and hence you don't need to have interpolation tags {{ }} in it. Attribute value will be parsed and evaluated by Angular, however {{ tags make value an invalid JS expression and cannot be processed.
Correct code would be
ng-if="index == 0 || index == 13 || index == 14 || index == 15 || index == 5 "

Related

convert this multiple condition into ng-class conditional

Hello there I need help in converting this 'if-else' with 'or' condition into conditon that can be used in ng-class.
This here is my ng-class with condition, but it's not working correctly.
<span ng-class="{'green': work > toWork,
'red': work < toWork,
'black': work == toWork || overall == '-'}">
{{overall = showMonthly(work = (workers | getMonthValue: dts.value),
toWork = getMonthlyToWork(member.id, dts.value))}}
</span>
this is the condition I'd like to apply:
if (work > toWork) {
return "green";
}else if (work < toWork) {
return "red";
}else if (work == toWork || overall == "-") {
return "black";
}
You don't need ng-class for that, you just need to put the logic inside a method in your $scope, like below
$scope.getClass = function(work, toWork, overall){
if (work == toWork || overall == "-"){
return "black";
}else if (work < toWork) {
return "red";
}else if(work > toWork) {
return "green";
}
}
and in your view, call it like this
<span class="{{getClass(work, toWork, overall)}}"></span>
<span ng-class="{'green': work > toWork,
'red': work < toWork,
'black': (work == toWork || overall == '-')}">
...
</span>
Check this. (You have got a spell missing in your conditional statement)
Happy coding!

ngClass if statement multiple expressions not working

I would like to give the the class 'bg-green' to the div when 'task.taskChecked' is true. This works, and also when 'task.taskDescription' has the word 'afspraak' the class should be 'bg-purple' this also works.
But with what I am struggling is, when 'task.taskChecked'is true AND 'task.taskDescription' contains the word 'afspraak' also give it the class 'bg-green'
Here is the code:
<div class="tableCell taskIndicator" data-ng-class="{'bg-green': task.taskChecked == true || ( task.taskChecked == true && task.taskDescription.toLowerCase().indexOf('afspraak') > -1 ), 'bg-red': task.taskChecked == false, 'bg-purple': task.taskDescription.toLowerCase().indexOf('afspraak') > -1}">
</div>
SOLUTION:
<div class="tableCell taskIndicator" data-ng-class="{'bg-green': task.taskChecked, 'bg-red': task.taskChecked == false, 'bg-purple': task.taskDescription.toLowerCase().indexOf('afspraak') > -1&&!task.taskChecked}">
</div>
order is important while writing your expression.
<div class="tableCell taskIndicator" data-ng-class="{'bg-red':
task.taskChecked == false, 'bg-purple':
task.taskDescription.toLowerCase().indexOf('afspraak') > -1,
'bg-green': task.taskChecked == true || ( task.taskChecked == true &&
task.taskDescription.toLowerCase().indexOf('afspraak') > -1 )}">
</div>

ng-show expression: comparing multiple strings using ||

Can someone please explain why this works:
<div ng-repeat="fruit in fruits">
<span ng-hide="fruit.type == 'apple' || fruit.type == 'banana'">
{{fruit.type}}
</span>
</div>
Renders:
pear lemon
But this doesn't:
<div ng-repeat="fruit in fruits">
<span ng-hide="fruit.type == 'apple' || 'banana'">
{{fruit.type}}
</span>
</div>
Renders:
// nothing
In Javascript (and most languages I know), comparisons aren't communicable. For every comparison (in this case equality), you unfortunately need a full statement.
In fruit.type == 'apple' || 'banana',
1. fruit.type == 'apple' is evaluated.
2. After that, it || compares the result of that to 'banana', which in Javascript is a truthy value ('' is the only "falsy" string, all other strings are "truthy").
In essence, you end up with fruit.type == 'apple' || TRUE, which will always trigger ng-hide.
in this case its a bad expression fruit.type == 'apple' || 'banana'
|| are used in javascript for "OR" and define a default value to a variable when some reference values is undefined or null for example:
var name = null || "mottaman85";
consolog.log(name);
"mottaman85"
function test(nombreV){
var name = nombreV || "mottaman85";
console.log(name)
}
test();
"mottaman85"

AngularJS ng-show ternary conditional with multiple conditions

Im working on a complex app where I need to disable a link if the ID sent from backend meets a certain criteria.
I'm using this now but not sure if it is correct:
ng-show="parentheaderData.casid === '807' || '806' || '808' ?false:true"
Does this look right?
Why don't you move this logic to a controller so you have
html :
ng-show="showParentheader(parentheaderData.casid)"
controller:
$scope.showParentheader = function(id) {
return ! (id === '807' || id ==='806' || id ==='808');
}
Thanks for all the support. The correct solution was:
ng-hide="parentheaderData.casid == '806' || parentheaderData.casid == '807' || parentheaderData.casid == '808'"
you can do like this:
ng-show="(parentheaderData.casid === '807' || parentheaderData.casid ==='806' parentheaderData.casid === || '808') ? false : true"
or:
ng-show=" !(parentheaderData.casid === '807' || parentheaderData.casid ==='806' parentheaderData.casid === || '808')"
ng-show="(parentheaderData.casid === '807' || parentheaderData.casid ==='806' parentheaderData.casid === || '808') ? false : true"

ng-class with scope that has multiple values

I would like to add a class based on the value of the scope.
It's not working :(
If the value.statut == 3, I would like to add class todo
Else If the value.statut == 2, I would like to add class done
Else If the value.statut == 1, I would like to add class doing
<span class="label" ng-class="value.statut == 3 ? 'todo' : ''; value.statut == 2 ? 'done' : ''; value.statut == 1 ? 'doing' : '';">Label</span>
The declaration needs to be slightly different:
<span class="label" ng-class="{'todo': value.statut == 3, 'done': value.statut == 2, 'doing': value.statut == 1}">Label</span>
So, the syntax is:
ng-class="{<className>: <predicate>, <className2>: <predicate2>, etc }"
Try this :
<span class="label" ng-class="{'todo': (value.statut == 3), 'done': (value.statut == 2), 'doing': (value.statut == 1)}">Label</span>
ng-class needs an object with key/value. The key is the class name and the value is a boolean.
Reference
https://docs.angularjs.org/api/ng/directive/ngClass

Resources