javascriptangularjsformsangularjs-forms

How do I get the value of a text field using AngularJS?


I want to get the value of a text field value using AngularJS when a form is submitted.

The code below always shows "undefined".

html:

<form ng-submit="AdvanceSearchSubmit($event)" name="AdvanceSearch" novalidate>
 <p ng-show="!AdvanceSearch.SearchKeyWord.$pristine && AdvanceSearch.SearchKeyWord.$invalid" class="text-danger">Text Cannot Be Empty!</p>
  <div ng-hide="AdvanceSearchModel" class="input-group">
    <input name="SearchKeyWord" ng-model="SearchKeyWord" id="SearchKeyWord" class="form-control" placeholder="Search in timeline...." type="text" required>
      <span class="input-group-btn" ng-click="isAdvanceSearch='false'; SearchPost(0,'true')">
       <button ng-disabled="AdvanceSearch.$invalid" type="submit" name="search" id="search-btn" class="btn btn-flat">
        <i class="fa fa-search"></i>
       </button>
      </span>
     </div>
</form>

one attempt:

$scope.AdvanceSearchSubmit = function(event)
{
    alert(event.target.value);
};

another attempt:

$scope.AdvanceSearchSubmit = function(event)
{
    alert(event.SearchKeyWord.value);
};

Solution

  • instead of event pass the SearchKeyWord as a parameter

    ng-submit="AdvanceSearchSubmit(SearchKeyWord)"
    

    controller

    $scope.AdvanceSearchSubmit = function(keyWord)
    {
        alert(keyWord);
    };