Now I am trying to add user information for a table, after I successfully send data,I have to refresh whole page to get update. I think I can use $scope.apply(), but where should I put it? in the promise? Here is my code:
Service
angular
.module('ecom')
.factory(
'PCservice',
[
'$q',
'sessionId',
function($q, sessionId) {
return {
loadPersonalCCList : function() { // This is how I get user information from server
var deferred = $q.defer();
$
.ajax({
type : "GET",
async: false,
url : ECOM_BASE_URL
+ "/esapp/EDService/personalcc/cards/list?sessionID="
+ sessionId,
dataType : "xml",
cache : false,
success : function(xml) {
var data = xml2json(
xml,
{
listPaths : [ /PERSONALCCLIST$/ ],
numPaths : []
}).OFX;
deferred.resolve(data);
}
});
return deferred.promise;
},
loadpaymentmethod:function(sessionId,companycode,paymenttype){
var deferred = $q.defer();
$
.ajax({
type : "GET",
async: false,
url : ECOM_BASE_URL+ "/esapp/EDService/system/compPayMeths",
dataType : "xml",
cache : false,
data: {
_session: sessionId,
companyCode: companycode,
paymentType: paymenttype
},
success : function(xml) {
console.log(xml);
/* var data = xml2json(
xml,
{
listPaths : [ /PERSONALCCLIST$/ ],
numPaths : []
}).OFX;*/
deferred.resolve(data);
}
});
return deferred.promise;
},
saveNewPC: function(xmlString){ // This is where I save the new user information
var deferred = $q.defer();
$.ajax({
type: "POST",
url: ECOM_BASE_URL+"/esapp/EDService/personalcc/cards/save",
contentType: "text/xml",
dataType: "xml",
cache: false,
processData: false,
data: xmlString,
//success: init
success: function (xml) {
//console.log(xml);
deferred.resolve(xml);
}
});
return deferred.promise;
}
}
} ]);
Controller
var promise = PCservice.loadPersonalCCList();
promise.then(function(data) {
$scope.personalccinfor = data.CCMSGSRSV1.PERSONALCARDLISTRS.PERSONALCCLISTINFO.PERSONALCCLIST;
$scope.cardinfo = data.SIGNONMSGSRSV1.SONRS.PROFILE;
});
$scope.savechange = function(cardname, paymethod, webaddress, checktype, txtCCNumber, reimbValue) {
var xmlstring = $scope.buildNewXML(cardname, paymethod, webaddress, checktype, txtCCNumber, reimbValue);
var feedback = PCservice.saveNewPC(xmlstring);
feedback.then(function(status) {
PCservice.loadPersonalCCList().then(function(data){
$scope.personalccinfor=data.CCMSGSRSV1.PERSONALCARDLISTRS.PERSONALCCLISTINFO.PERSONALCCLIST;
$scope.cardinfo = data.SIGNONMSGSRSV1.SONRS.PROFILE;
$scope.$apply();
});
});
};
HTML
<tr ng-repeat='pcdata in personalccinfor'>
<td class='text-center'><a href="">{{pcdata.NAME}}</a><input type="hidden" name="cardId" id="cardId" value="{{pcdata.CARDID}}}"></td>
<td class='text-center'>{{pcdata.WEBADDRESS}}</td>
</tr>
As you can see, I need to init page with data, then if user want to add new information, I need to re-call the loadPersonalCCList()
to init page .