Processing $http response in service

后端 未结 12 1942
半阙折子戏
半阙折子戏 2020-11-22 03:36

I recently posted a detailed description of the issue I am facing here at SO. As I couldn\'t send an actual $http request, I used timeout to simulate asynchrono

12条回答
  •  天命终不由人
    2020-11-22 04:29

    When binding the UI to your array you'll want to make sure you update that same array directly by setting the length to 0 and pushing the data into the array.

    Instead of this (which set a different array reference to data which your UI won't know about):

     myService.async = function() {
        $http.get('test.json')
        .success(function (d) {
          data = d;
        });
      };
    

    try this:

     myService.async = function() {
        $http.get('test.json')
        .success(function (d) {
          data.length = 0;
          for(var i = 0; i < d.length; i++){
            data.push(d[i]);
          }
        });
      };
    

    Here is a fiddle that shows the difference between setting a new array vs emptying and adding to an existing one. I couldn't get your plnkr working but hopefully this works for you!

提交回复
热议问题