问题描述:

I have the following code for an application, which loads data from http.

Initial json-data:

"phasen": {

"item1": false,

"item2": [{

"phasen_id": "1",

"phasen_group_id": "2",

"phasen_group_name": "groupx",

"phasen_name": "name1",

"phasen_data": {

"phasen_status": "1",

"user_modified_id": "10",

"user_modified_name": "Admin ",

"createdate": "17.06.2016 | 10:19"

}

}, {

"phasen_id": "2",

"phasen_group_id": "2",

"phasen_group_name": "name2",

"phasen_data": false

}, {

"phasen_id": "3",

"phasen_group_id": "2",

"phasen_group_name": "name3",

"phasen_data": false

}

]

}

html-code:

<div ng-repeat="myData in dataSet.phasen.item2">

<div>

<input type="checkbox" ng-model="myData.phasen_data.phasen_status" ng-true-value="'1'" ng-false-value="'0'" ng-checked="myData.phasen_data.phasen_status == '1'" ng-change="savedata('{{myData.phasen_group_id}}','{{myData.phasen_id}}', myData.phasen_data.phasen_status)" icheck />

</div>

<div class="info-box-content">

<span class="info-box-text">{{myData.phasen_name}}</span>

<span class="info-box-number">

<div ng-show="myData.phasen_data.phasen_status == '1'" class="small">{{myData.phasen_data.createdate}} | {{myData.phasen_data.user_modified_name}}</div>

</span>

</div>

</div>

And the Javascript:

$scope.savedata = function(phasen_group_id, phasen_id, phasen_status){

$http({

method: 'POST',

url: base_url + "dataapi/savedata/",

data: { 'phasen_group_id': phasen_group_id, 'phasen_id': phasen_id, 'phasen_status': phasen_status},

headers: {

'Accept': 'application/json',

'Content-Type': 'application/json; charset=utf-8'

}

}).then(function(response){

var data = response.data;

console.log(data);

$parse('dataSet.phasen').assign($scope, data);

});

};

Is it possible to save the data and to update only one item in ng-repeat with the response data from http?

网友答案:

Yes it is. Just pass whole single object to the savedata function. In your case it is ng-repeat="myData in dataSet.phasen.item2" so you need to pass mydata.

savedata(myData);

In success just assign response object to the myData and it will be automatically updated in the view because when you pass whole object from the ng-repeat it is passed by reference, not by value.

网友答案:

Change your HTML to:

<div ng-repeat="myData in dataSet.phasen.item2">

<div>
    <input type="checkbox" ng-model="myData.phasen_data.phasen_status" ng-true-value="'1'" ng-false-value="'0'" ng-checked="myData.phasen_data.phasen_status == '1'" ng-change="savedata(myData)" icheck />
</div>

<div class="info-box-content">
    <span class="info-box-text">{{myData.phasen_name}}</span>
    <span class="info-box-number">
        <div ng-show="myData.phasen_data.phasen_status == '1'" class="small">{{myData.phasen_data.createdate}}  |  {{myData.phasen_data.user_modified_name}}</div>
    </span>                         
</div>  

And your JavaScript:

$scope.savedata = function(myData){

$http({
    method: 'POST',
    url: base_url + "dataapi/savedata/",
    data: myData
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8'
    }
}).then(function(response){
    var data = response.data;
    console.log(data);
    $parse('dataSet.phasen').assign($scope, data);  
});

};

相关阅读:
Top