2017-05-08 73 views
2

我不知道爲什麼我的列表沒有更新,點擊了add。它只在刷新時更新。這裏是我的代碼爲什麼我的列表只在刷新時更新

服務

function getCustomer() { 
     return $http.get(API_URL+'api/customer') 
        .then(getCustomerSuccess) 
        .catch(function(err) { 

          }); 
       function getCustomerSuccess(response) { 

        return response.data; 
        } 
        } 

的js

function getCustomer() { 
         dataServices.getCustomer() 
           .then(function(response){ 
        vm.customer = response; 
}) 
       } 

       vm.add = function(customer) { 
        dataServices.addCustomer(customer); 
        getCustomer(); 
        logger.success('Add Customer Success'); 
       } 

回答

1

爲什麼我的列表只更新時刷新

的問題是:你解決無極裏面服務並返回對象,而不是Promise。刷新後,觸發摘要循環,從服務器獲取新加載的數據。

從您的代碼中,a.e. dataServices.getCustomer().then(/* .. */)你期望無極

使用$q

,可以幫助您運行功能異步運行,並利用他們的 返回值(或例外),他們都完成處理後的服務。

function getCustomerSuccess(response) { 
    var deferred = $q.defer();    
    deferred.resolve(response.data);    
    return deferred.promise;  
} 
+0

謝謝建議我會試試看。但是,你能解釋一下爲什麼在這種情況下應該使用$ q –

+0

@NguyễnBảoDũng查看這個例子:http://jsfiddle.net/oymubx4z/ –

+0

這不是工作。我正在嘗試使用不同的文件進行測試,它的工作原理。但在我的文件中,這是行不通的。我使用CORS,所以CORS與此相關或不相關? –