0

I am using angular js. How can i call a function if the length of the input text id greater than 5.

<input  class="form-control" style="width:230px" ngmodel="Lookupemployer" ng-if={{ }}  />

I want to call the below function

$scope.get = function()
{


}

if the length of the Lookupemployer is greater than 5.

I tried like this

ng-if={{ Lookupemployer.length >5 ? get() : ""}}

but did not work. Can anyone help me

Thanks,

3
  • what is Lookupemployer? is it a string, an object, an array, a function? it's not really clear here, either from the code or from the name. The name makes me think function, but the use makes me think string. Commented Feb 20, 2015 at 5:36
  • 1
    You are not understanding what ng-if is doing. ng-if determines if to render the element based on the condition Commented Feb 20, 2015 at 5:39
  • I think you should handle this input length condition inside the function. If condition evaluates to true, you can proceed with the function code else just return Commented Feb 20, 2015 at 5:42

2 Answers 2

1

The ng-if directive is used to determine whether or not to create the element in the DOM. It is not an actual conditional statement to be used to execute code.

Read the documentation for ng-if for further details: https://docs.angularjs.org/api/ng/directive/ngIf

You want to use ng-change. Call a method in that and have the function do the actual checking of the input length. See the docs for more info: https://docs.angularjs.org/api/ng/directive/ngChange

Sign up to request clarification or add additional context in comments.

Comments

0

First, you need to use ng-change - it will execute an expression on every change in the bound model.

You could do this all from the View, like so:

<input ng-model="foo" ng-change="foo.length > 5 && get()">

But it's better and cleaner to keep the logic out of the View (unless you consider it to be View-specific).

$scope.getIfNeeded = function(){
  if ($scope.foo.length > 5) get();
}

And the View:

<input ng-model="foo" ng-change="getIfNeeded()">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.