我想顯示控制器中只有更新的值,但我得到完整的數據。我怎樣才能得到更新的值?如何在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"
}]
}
]
檢查每個字段的$ pristine屬性。請參閱http://stackoverflow.com/questions/18641618/how-can-i-denote-which-input-fields-have-changed-in-angularjs –