2015-10-26 90 views
0

我想在我收到数据并存储它之后使用函数,我怎么得不到我想要的结果以及我使用的代码。在http请求后调用

有人可以提供一些关于我在做什么错的细节吗?

我的代码:

$http({ 
     method: 'GET', 
     url: 'JsonLocation' 
    }).then(
     function successCallback(response) { 
      // this callback will be called asynchronously 
      // when the response is available. 
      self.responseData = response.data; 
     }, 
     function errorCallback(response) { 
      // called asynchronously if an error occurs 
      // or server returns response with an error status. 
      console.log(response); 
     } 
    ).then(
     function completeCallback() { 
      // called when the http request is finished. 
      restyleData(); 
     } 
    ); 

当加载页面时,一切都设在地方,我可以运行通过开发者选项restyleDate(),那么它的工作原理。但是我只是想在加载完所有内容后才开启它,而不是手动完成。

+0

删除第二个'.then'语句,并将你的'restyleData'方法放入你的'successCallback' –

+0

那么它有什么特殊的问题呢?这个函数做什么? '我没有得到想要的结果'并不是非常明确 – charlietfl

+0

@ChristopherMarshall这个函数似乎在所有数据都在页面之前被调用。我认为我的问题在于我在'restyle'之前处理来自角的ng-repeat。我需要在页面上显示所有数据后才能看到它。 – Marcel

回答

1

我会建议更改为:

$http({ 
    method: 'GET', 
    url: 'JsonLocation' 
}).then(
    function successCallback(response) { 
     // this callback will be called asynchronously 
     // when the response is available. 
     self.responseData = response.data; 
     // restyle is called after you save the result 
     restyleData(); 
    }, 
    function errorCallback(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
     console.log(response); 
    } 
); 
0

使用catchfinally使用再经过。如果你做$scope.responseData = data,那么可以在你的HTML中使用,并且数据会自动填充。所以在html中它看起来像{{responseData.description}}。如果你已经将自己定义为:{{self.responseData.description}}

或者,你可以将你的$ http调用包装在一个名为restyleData的函数中,然后调用该函数(就像你一样),它将显示数据。

$http({ 
    method: 'GET', 
    url: 'JsonLocation' 
}).then(
    function successCallback(response) { 
     // this callback will be called asynchronously 
     // when the response is available. 
     self.responseData = response.data; 
    }) 
    .catch( 
    function errorCallback(response) { 
     // called asynchronously if an error occurs 
     // or server returns response with an error status. 
     console.log(response); 
    }) 
    .finally(
    function completeCallback() { 
     // called when the http request is finished. 
    }); 

restyleData();