2016-04-24 17 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'; 
      } 
     } 
    } 
}