0

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 .

linyuanxie
  • 777
  • 4
  • 13
  • 31

1 Answers1

1

Your problem is you are using $.ajax which is jquery, after making an ajax angular doesn't get any idea about should i need run my digest cycle to update my scope variable. So thats why angular does have $http which make ajax call and after that run digest cycle and update all scope variables.

$http

It is same as that of $.ajax, but this function wrap inside a $apply() function which call $digest cycle, and $digest cycle updates all the variables throughout the scope.

Covert your $.ajax to $http call will solve your binding issue.

There is way to update binding by manually running $apply() method to forcefully update the scope level binding

if(!$scope.$$phase) //checking if any digest cycle is running or not
  $scope.$apply();

But but don't use $scope.$apply() its anti pattern in Angular JS

Community
  • 1
  • 1
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299