重置按钮不更新AngularJS中的值

时间:2017-06-08 14:49:39

标签: javascript angularjs

我在这上面空白,它看起来应该有用,但是......

我有一个按钮:

 <button class="btn btn-warning" type="reset" ng-click="reset()">Clear </button>

复位功能的简化版本:

  $scope.reset = function() {
        $scope.application = {};
        $scope.PostDataResponse = null;
        $scope.errors = null;
        $scope.selectedApplication = null;
        $scope.application.status = true;
        $scope.application.version = 1.0;
        $scope.gridOptions.data.length = 0;
        $scope.ApplicationSettingsForm.$setPristine();
        $scope.ApplicationSettingsForm.$setUntouched();
    };

输入我正在尝试重置:

<input type="number" style="font-family: sans-serif" class="form-control" ng-model="application.version" ng-disabled="true" id="Version" name="Version" placeholder="Version..." required>

首次加载页面时如何播种:

    $scope.application = {};
    $scope.application.version = 1.0;

当页面加载时我得到1,但是当我点击重置时,它会恢复为版本...而不是预期的1.我在这里做错了什么?

2 个答案:

答案 0 :(得分:0)

reset()函数中的一些代码行可能会抛出异常。很可能它可能是$scope.gridOptions.data.length = 0;如果不是这样,请检查控制台,看看是否有其他行抛出任何异常。

如果reset()函数中的某些行抛出异常,则可能是应用程序对象的版本未正确设置。

您可以尝试将代码更改为此吗?我添加了一些空检查,希望避免任何例外:

  $scope.reset = function() {
        $scope.application = {};
        $scope.application.status = true;
        $scope.application.version = 1.0;
        $scope.PostDataResponse = null;
        $scope.errors = null;
        $scope.selectedApplication = null;
        if($scope.gridOptions && $scope.gridOptions.data) {
             $scope.gridOptions.data.length = 0;
        }
        if($scope.ApplicationSettingsForm) {
          $scope.ApplicationSettingsForm.$setPristine();
          $scope.ApplicationSettingsForm.$setUntouched();
        }
    };

答案 1 :(得分:0)

您的代码有效,但您必须将defaultdict定义为控制器中的对象:

&#13;
&#13;
$scope.application
&#13;
var myApp = angular.module('myApp', []);

myApp.controller('FirstCtrl', function($scope) {
   $scope.application = { version: 1.0 };

   $scope.reset = function() {
     $scope.application = {};
     $scope.application.version = 1.0;
   };
});
&#13;
&#13;
&#13;