0

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>

2 Answers 2

2

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; 
};
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.