2017-09-16 56 views
0

任何人都可以幫助我如何創建多個函數Angular JS Factory,我想訪問一個函數返回的值並處理另一個函數,我在下面試過,但沒有成功如何定義多個函數並在Angular JS中的其他函數中調用一個函數

在以下功能我想取在modifyProduct函數值,我們從getProduct得到(response.data)

我在下面提及的問題,但沒有得到多少了解

AngularJS : From a factory, how can I call another function

Calling a function in another function with AngularJS factory

app.factory('ProductsService', function($http) { 
    function getProduct() { 
    return $http.get('finalmsodetails.json').then(function(response) { 
     console.log(response.data); 
     return response.data; 
    }); 
    } 

    function modifyProduct() { 
    this.getProduct().then(function(value) { 
     console.log(value); 
    }); 
    } 
    return { 
    getProduct: getProduct, 
    modifyProduct: modifyProduct 
    }; 
}); 

回答

1

你靠近。只需要刪除this.,因爲您調用的是在本地範圍內定義的另一個函數,而不是對象上的某種方法。我想你可能會想讓modifyProduct返回promise,這樣任何人調用這個函數都可以知道它何時成功或失敗。

function modifyProduct() { 
    return getProduct().then(function(value) { 
    console.log(value); 
    }); 
} 
+0

我已經試過這@Nicholas,但沒有值打印的價值,不知道爲什麼 – Batman

+0

呀日誌priniting對'的console.log(response.data);'裏面getProduct – Batman

+0

所以後來似乎變工作,並且modifyProduct正在調用getProduct。但它沒有任何意義,getProduct會解決它的承諾,但不會轉發...嗯... –

相關問題