0

我在更新 AngularJS 控制器中的值并让它们正确反映在我的模型中时遇到问题。

form在我看来,我有以下几点:

<form name="formSignIn" id="formSignIn" role="form">
  <div class="form-group">
    <label for="guestName">Name or Employee ID</label>
    <div class="input-group merged">
      <input type="text" class="form-control" id="guestName" ng-model="name" typeahead="person.name as person.displayName for person in getTypeaheadValues($viewValue)" typeahead-on-select="onTypeaheadSelect($item, $model, $label)" placeholder="Enter Name or Employee ID">
      <span class="input-group-btn">
        <button type="button" class="btn btn-default"><i class="fa fa-times-circle"></i></button></span>
    </div>
  </div>
  <div class="form-group">
    <label for="guestOrg">Organization</label>
    <input type="text" class="form-control" id="guestOrg" ng-model="organization" placeholder="Organization">
  </div>
  <div class="form-group">
    <label for="guestPurpose">Purpose</label>
    <input type="text" class="form-control" id="guestPurpose" ng-model="purpose" placeholder="Why are you visiting today?">
  </div>
</form>

ng-model我在控制器顶部初始化我的值:

$scope.name = "";
$scope.organization = "";
$scope.purpose = "";

然后,我有两个函数可以清除值,基于访问者点击“清除”或“登录”按钮:

$scope.onSignInClick = function()
{
    // show confirmation
    $scope.leftContent = "confirm";

    // clear the form
    $scope.name = null;
    $scope.organization = null;
    $scope.purpose = null;

    // display the form again after a short pause
    $timeout(function () {
        $scope.leftContent = "splash";
    }, 3000);
}

$scope.onClearClick = function()
{
    // confirm function is being called
    console.log("clear");

    // clear the form (trying multiple ways)
    $scope.name = "";
    $scope.organization = null;
    $scope.purpose = "abc123";
}

我的onSignInClick功能...工作无误。没有麻烦。

我的onClearClick功能......从来没有以一致的方式工作。有时organization被清除,有时purpose被填充(我正在更新上面代码中的值以表明只是“清除”该值不是问题)。根据我输入的字段或通过预先输入自动填充的字段,结果会onClearClick有所不同。

我曾尝试删除 typeahead(AngularUI-Bootstrap Typeahead 指令),但这并没有解决问题。

为什么我的值有时会在视图中更新,有时不会?

4

0 回答 0