2016-04-24 55 views
-1

http://jsfiddle.net/r2ohsuty/检查到任何输入所做的更改的Fileds

尽量选用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'; 
     } 

    } 
    } 
} 
+2

变化相比有哪些?你正在比较当前值和当前值,这总是正确的。 – JJJ

+0

什么是用例? – Tushar

+0

@Juhana当前值与新提交的值进行比较。我认为如果用户更改输入值,范围[键]将会更新? – Jennifer

回答

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'; 
      } 
     } 
    } 
}