检查是否对任何输入文件进行了更改

时间:2016-04-24 15:22:46

标签: javascript angularjs

http://jsfiddle.net/r2ohsuty/

尝试使用angularjs检查用户是否对任何输入字段进行了任何更改,但不确定我搞砸了哪里,它没有按预期工作。

function AppCtrl($scope) {

  $scope.submit = function(name, age, gender) {

    var fields = {
      name: name,
      age: age,
      gender: gender
    };
    for (key in fields) {
      if (fields[key] === $scope[key]) {
        $scope.result = 'no changes';
      } else {
        $scope.result = 'have changes';
      }

    }
  }
}

1 个答案:

答案 0 :(得分:0)

我相信这就是你想要的:

function AppCtrl($scope) {
    $scope.originalValues = { name: "original name value", age: "original age value", gender: "origin gender value" }
    $scope.updatedValues = angular.copy($scope.originalValues)
    // Within your view. set ng-model of each input to the corresponding field 
    // Eg ng-model="updatedValues['name']"

    $scope.submit = function() {
        for (key in $scope.originalValues) {
            if ($scope.originalValues[key] === $scope.updatedValues[key]) {
                $scope.result = 'no changes';
            } else {
                $scope.result = 'have changes';
            }
        }
    }
}