2015-07-04 52 views
0

我想顯示控制器中只有更新的值,但我得到完整的數據。我怎樣才能得到更新的值?如何在angularjs中只顯示更新的表單數據?

輸入字段:

<div class="" ng-repeat="fields in containerDetails.containerFields"> 
    <input type="{{features.input}}" minlength="{{features.rules[0].minlength}}" maxlength="{{features.rules[0].maxlength}}" required name="{{name}}" ng-model="features.name" > 
    </div> 
<button ng-click="save()">Save</button> 

單擊Save應該只能傳遞更新值到控制器。

.controller('DemoCtrl', function($scope, $http) { 

      $http.get('json/test.json'). 
      success(function(data, status, headers, config) { 
       $scope.containerDetails=data; 

       console.log(data); 
       $scope.mode=data.containerProperties.mode; 
       $scope.user=data.containerProperties.role; 
       console.log($scope.user); 
       }). 
      error(function(data, status, headers, config) { 
       console.log("error"); 
      }); 


    $scope.save = function() { 

    $scope.msg = 'Data sent: '+ JSON.stringify($scope.containerDetails); 

     console.log($scope.msg); 
    }; 

我喜歡JSON:

"containerFields": [ 

     { 
     "label": "First Name", 
     "name": "first_name", 
     "placeholder": "Enter Your first name", 
     "primary": "no", 
     "required": "yes", 
     "type": "text", 
     "input":"text", 
     "enum": "false", 
     "access":"user", 
      "minlength":"10", 
      "maxlength":"150", 
     "rules": [{ 
      "message":"Enter First Name", 
      "minlength":"10", 
      "maxlength":"150", 
      "elementValidate":"aplha" 
     }] 
     }, 
     { 
     "label": "Email", 
     "name": "email", 
     "placeholder": "Enter Your email", 
     "primary": "no", 
     "required": "yes", 
     "type": "text", 
     "input": "email", 
     "access":"user", 
     "rules": [{ 
      "message":"Enter valid email", 

      "elementValidate":"email" 
     }] 
     } 
] 
+0

檢查每個字段的$ pristine屬性。請參閱http://stackoverflow.com/questions/18641618/how-can-i-denote-which-input-fields-have-changed-in-angularjs –

回答

0

寫值在一個單獨的數據結構。安全功能可通過$scope.data訪問所有值。

<input type="{{features.input}}" minlength="{{features.rules[0].minlength}}" maxlength="{{features.rules[0].maxlength}}" required name="{{name}}" ng-model="data[features.name]" > 
+0

感謝您的回覆。請解釋我如何以這種方式做。謝謝 – stackoverflow

+0

看看我在答案中提供的代碼。使用'ng-model =「data [features.name]」'而不是'ng-model =「features.name」' – Michael

相關問題