I have the following Angular code:
<li ng-repeat="select in Items">
<foo ng-repeat="newin select.values">
{{new.label}}</foo>
How can I use an ng-if condition to look for a specific character:
ng-if="select.name == '?'"
to only display the code when the character is here? The value I have is like 88?77 and the numbers are dynamic but the question mark is always there, I can't seem to filter based on that?
You can use indexOf
to check if the character is contained by string
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found.
ng-if="select.name.indexOf(myChar) > -1"
Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
ng-if="select.name.indexOf('?') !== -1"
Do checks like that in a controller function. Your HTML should be easy-to-read markup without logic.
Controller:
angular.module("myApp")
.controller("myController",function(){
var self = this;
self.select = { /* ... */ };
self.showFoo = function() {
//Checks if self.select.name contains the character '?'
return self.select.name.indexOf('?') != -1;
}
});
Page example:
<div ng-app="myApp" ng-controller="myController as vm">
<p ng-show="vm.showFoo()">Bar</p>
</div>
All javascript methods are applicable with angularjs because angularjs itself is a javascript framework so you can use indexOf() inside angular directives
<li ng-repeat="select in Items">
<foo ng-repeat="newin select.values">
<span ng-if="newin.label.indexOf(x) !== -1">{{newin.label}}</span></foo>
</li>
//where x is your character to be found