如何使用AngularJS获取文本字段的值?

时间:2017-07-01 06:57:06

标签: javascript angularjs forms angularjs-forms

我希望在提交表单时使用AngularJS获取文本字段值的值。

以下代码始终显示" 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>

一次尝试:

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

另一次尝试:

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

2 个答案:

答案 0 :(得分:0)

而不是eventSearchKeyWord作为参数传递

ng-submit="AdvanceSearchSubmit(SearchKeyWord)"

控制器

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

答案 1 :(得分:0)

在提交时,您根本无需将活动传递给AdvanceSearchSubmit。您已将$scope内的值设置为ng-model,用于输入字段ng-model="SearchKeyWord"

alert($scope.SearchKeyWord); //access value from `$scope` directly