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

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

我想在提交表单时使用 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);
};

而不是event传递SearchKeyWord作为参数

ng-submit="AdvanceSearchSubmit(SearchKeyWord)"

控制器

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

您根本不需要在提交时将事件传递给您的 AdvanceSearchSubmit。对于 ng-model="SearchKeyWord"

这样的输入字段,您已经在 $scope 中使用了您的值,例如 ng-model
alert($scope.SearchKeyWord); //access value from `$scope` directly