Nymeria
Nymeria

Reputation: 277

AngularJS search string in ng-if

I need to know if a string containts another string in "ng-if" instruction

 <span ng-if="list.alignement.search(/leftright/i) > -1">
                <span class="pull-left" ng-bind="list.data[0]"></span>
                <span class="pull-right" ng-bind="list.data[1]"></span>
            </span>

I have this error :

http://errors.angularjs.org/1.2.19/$parse/syntax?p0=leftright&p1=is%20unexpected%2C%20expecting%20%5B)%5D&p2=25&p3=list.alignement.search(%2Fleftright%2Fi)%20%3E%20-1&p4=leftright%2Fi)%20%3E%20-1

I try to cast it into String but it doesn't work :

<span ng-if="String(list.alignement).search(/leftright/i) > -1">
                <span class="pull-left" ng-bind="list.data[0]"></span>
                <span class="pull-right" ng-bind="list.data[1]"></span>
            </span>

Upvotes: 0

Views: 2068

Answers (2)

harishr
harishr

Reputation: 18055

you can create factory for this

app.factory("stringContains", function(){
   return function(input, value){
        return (input.indexOf(value) > -1)
   }
}

you can use it like this

       <span ng-if="list.alignement | stringContains : 'leftright' ">
            <span class="pull-left" ng-bind="list.data[0]"></span>
            <span class="pull-right" ng-bind="list.data[1]"></span>
        </span>

Upvotes: 1

EpokK
EpokK

Reputation: 38092

You need to call external function in your controller like this:

view

<span ng-if="foo(list)">

controller

$scope.foo = function(list) {    
    return String(list.alignement).search(/leftright/i) > -1; 
};

Upvotes: 2

Related Questions