The AngularJS Noob Handbook has some code which reduces class manipulation to a simple expression and binding :
<a ng-click="flags.open=!flags.open">...<div ng-class="{active:flags.open}">
However, what is the expression syntax in ng-class? I understand that a vertical bar (|) would pass through a filter and that a filter can be passed parameters after a colon but the above code is doing something different. If the scope variable on the right evaluates to true then the expression on the left is included otherwise it's dropped.
Is this specific to the ng-class directive? Is there some documentation on http://docs.angularjs.org that explains this?
This is mentioned briefly (too briefly, in my opinion) in the ngClass documentation. If you pass an object to ngClass, then it will apply each key of the object as a class to the element if that key's value is true. For example:
$scope.first = true
$scope.second = false
$scope.third = true
with
<div ng-class="{a: first, b: second, c: third}"></div>
would result in
<div class="a c"></div>
you've probably also seen something like this:
<div ng-class="{true: 'complete'}[item.Id != 0]"></div>
Very rad syntax.
EDIT:
What happens here, is that the "complete" class is added to the element if(item.Id != 0). Alternatively, we could write: <div ng-class="{false: 'cookieless'}[monsterEatsCookies('Elmo')]. As its decided by the monsterEatsCookies function, Elmo does not eat cookies so since this function returns false the html element gains a class called cookieless.
A simple example: <div ng-class="{false: 'DoubleNegative'}[1 == 0]. 1 !== 0 which is "false" -- the "DoubleNegative" class is added to the element.
<div ng-class="{ true: 'complete' } [item.Id != 0]"></div>
`
| | | | | | | |
| |result| |className | | | |
| | | | |
| function | | | condition |
Addendum
Also, I just realized that you can use a variety of different keys to map to your condition. For example:
ng-class="{ true: 'highlight', undefined: 'mute' }[ item.hasValue ]"
The mute class will be applied if item has no "hasValue" property. Furthermore, you can apply a class for any given type or value:
{'Jonathan Chapman': 'embolden', '[object Object]': 'hide'}[ item.toString() ]
In the following collection, this would embolden a person's name while hiding items that are objects:
[
'Jonathan Chapman',
{ aka: 'Johnny Applyseed' },
'Brad Pitt',
{ details: 'Fights Zombies' }
]
With this, you could watch for specific values in any $scope property. I suppose this could come in very handy at times.
Cheers
ng-click="flags.open=!flags.open"
switch the value of the flags.open to true or false.
And
ng-class="{active:flags.open}"
decides whether the class active is present or not based on the value of flags.open.
Please see the Fiddle demonstrating the above example.
like this example below :
div(ng-class=" condition ? ['class_one', 'class_two'] : ['class_three', 'class_four']")
Here's how you can pass expression with filter:
<div ng-class="{ 'customer-page': ('customer' | isRoute),
'orders-page': ('orders' | isRoute) }">....</div>
Related
How to filter my object inside expression value?
I have this ng-repeat
ng-repeat="(series, detailData) in projectDetails"
the return of detailData is object which is i need to filter to get exactly field that i need.
{"projectViewType":"card1",
"projectName":"Hamilton",
"projectType":"Onprocess"},
{"projectViewType":"card2",
"projectName":"Christone",
"projectType":"Done"},
{"projectViewType":"card2",
"projectName":"Villas",
"projectType":"Done"} .... more
I need to filter this result with projectType = Done
and return all object with projectType = Done
You can try this
<div ng-repeat="project in projectDetails| filter: { projectType : 'Done' }">
Can't you just add a standard filter?
ng-repeat="(series, detailData) in projectDetails | filter:{'projectType':'Done'}"
I don't even think you'd need a custom filter.
Check the docs here https://docs.angularjs.org/api/ng/filter/filter especially the part where it describes the expression argument.
ng-repeat="(series, detailData) in projectDetails | filter: projectType == "Done" "
I'm trying to get a zero value to show up in angular UI.
<ng-show ng-show="item._source.change.backOrdered"><br> {{'pages.items.backOrdered' | translate}} {{item._source.change.backOrdered}}</ng-show>
So there's 2 cases - when the value is undefined it shouldn't display. But when the value is 0 it should be displayed.
Is there a trick to make this work, like convert the zero to string or something?
Use ng-show=var != null:
<ng-show ng-show="item._source.change.backOrdered != null">
<br> {{'pages.items.backOrdered' | translate}} {{item._source.change.backOrdered}}
</ng-show>
i think you can use angular.isNumber
js:
$scope.isNumber = function(value){
return angular.isNumber(value);
}
html:
<ng-show ng-show="isNumber(item._source.change.backOrdered)"><br> {{'pages.items.backOrdered' | translate}} {{item._source.change.backOrdered}}</ng-show>
JS:
var MyApp = angular.module('SOFdemo',['angular.filter']);
MyApp.controller('SOFCtrl',['$scope','$resource','$location',function($scope,$resource,$location){
$scope.serverSizeOptions = [
{"name":"20 Users","value":20},
{"name":"30 Users","value":30},
{"name":"40 Users","value":40},
{"name":"50 Users","value":50},
{"name":"60 Users","value":60},
{"name":"70 Users","value":70},
{"name":"80 Users","value":80},
{"name":"90 Users","value":90}];
}]);
I want to display on a table the possible options for a customer that subscribe to a max and min size. I'm using the directive angular-filter
I assigned the controller to my body.
The following ng-repeat directive usage on my partial HTML working well for a tr:
< tr ng-repeat="(key,value) in (serverSizeOptions | omit: value<20 | omit: value>50)">
< td>{{key}}
< td>{{value}}
< /tr>
Now, I would like to change 20 or 50 by my project size like:
< tr ng-repeat="(key,value) in (serverSizeOptions | omit: value<20 | omit: value> project.size.value)"> ...
But here, filter is not applied and I have all my list.
project.size.value is on my current scope.
I tried several configuration since hours and I'm starting to think that it's not possible to do what I'm expecting.
Any help will be appreciate.
You can use the filterFilter and define a function on your scope, that selects the valid values.
ng-repeat = "item in serverSizeOptions | filter:myFunction"
$scope.myFunction = function(item) { ..your logic here return a boolean }
Is there a way to type:
<div ng-repeat="item in items | limitTo:what">
where I can substitute "what" with something that will make it iterate through the whole list of items. (note items.length is not what I am searching for.. or it must be with some ugly if inside the html).
In the source for limitTo there is support for an infinite number (Infinity):
if (Math.abs(Number(limit)) === Infinity) {
limit = Number(limit);
} else {
limit = int(limit);
}
Looks like you should be able to set to Number.POSITIVE_INFINITY.
However, the resulting code would probably be no better than using items.length. And would certainly be less understandable.
you don't need {{ }}, here is the documentation
<div ng-repeat="item in items | limitTo:what">
in the controller
$scope.what = 3; // iterate only 3
This is simple man
<div ng-repeat ="item in items | limitTo:10">
or $scope.what = '10'; and use what in limit.
I'm using Angular 1.2.10 (~ Stable ~)
I have something like this :
<tr ng-repeat="server in servers.hits.hits | orderBy: type.id">
My server object look like this :
server = {
name = "String",
type = {
id: Integer,
label: "String"
},
url: "String"
}
My server list is not organized properly but in an Angular-UI < select > it works.
Plus I don't know if it's possible but I sometimes need to put a double orderBy
( | orderBy:['id', 'label'] ) but also it won't work.
Anyone knows what's wrong ?
Thanks
Try this
<tr ng-repeat="server in servers.hits.hits | orderBy:'type.id'">
see this fiddle: http://jsfiddle.net/bDPW2/1/
More info from the docs:
filter:orderBy(array, expression[, reverse]);
A string as an expression:
string: An Angular expression which evaluates to an object to order by, such as 'name' to sort by a property called 'name'. Optionally prefixed with + or - to control ascending or descending sort order (for example, +name or -name).