1

I have an input and a button

<input type="text" name="search_tags" id="search_tags" placeholder="Search by Tags" class="form-control"  >
<button type="submit" class="btn btn-primary btn-lg" ng-click="searchTags()">Search</button>

How do I pass the text present in the input textbox to the searchTags() function after button ng-click?

1
  • Try to add ng-model attribut to the input tag : <input type="text" name="search_tags" id="search_tags" ng-model="myText" placeholder="Search by Tags" class="form-control" >. Then you can access myText value in the searchTags() method using the $scope service : $scope.myText. Commented Jun 13, 2016 at 11:47

4 Answers 4

4

Set ngModel directive with the input control, then pass that to ngClick of button.

<input type="text" ng-model="myText">
<button type="submit"  ng-click="searchTags(myText)">Search</button>

The property myText will be accessible in $scope

jsFiddle

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

Comments

0

You need to reference a ng-model for your input field like that:

<input ng-model="myScopeVariable" type="text" name="search_tags" id="search_tags" placeholder="Search by Tags" class="form-control" >
<button type="submit" class="btn btn-primary btn-lg" ng-click="searchTags(myScopeVariable)">Search</button>

Comments

0

You are using angularJs. So you can give ng-model to the input so that you can access the value through it.

One way to access is

<input type="text" ng-model="text_content" placeholder="Search by Tags" class="form-control">
<button type="submit" class="btn btn-primary btn-lg" ng-click="searchTags(text_content)">Search</button>

otherwise, you can just access the value in controller with out passing in the function.

<input type="text" ng-model="text_content" placeholder="Search by Tags" class="form-control">
<button type="submit" class="btn btn-primary btn-lg" ng-click="searchTags()">Search</button>

You can access the value in controller by $scope.text_content

Comments

0

Use the ng-model directive to bind data from the model to the view on HTML controls (input, select, textarea)

The ng-model directive provides a two-way binding between the model and the view.

<input ng-model="search_tag" type="text" name="search_tags" id="search_tags" placeholder="Search by Tags" class="form-control"  >
<button type="submit" class="btn btn-primary btn-lg" ng-click="searchTags(search_tag)">Search</button>

Comments

Your Answer

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