所以這是我的工廠代碼:運行代碼後先
app.factory('simpleFactory', function ($http) {
var factory = {};
factory.getArray = function (srchWord) {
**Here i have a code that uses $http to fill a array called result with values.
return result;
};
return factory;
});
這是我的範圍內的代碼:
$scope.search = function() {
$scope.arrayValue = simpleFactory.getArray($scope.searchWord);
$scope.booleanValue = ($scope.arrayValue.length <= 0); // <-- PROBLEM! This gets executed before getArray() is finished.
};
我的問題是$scope.booleanValue = ($scope.arrayValue.length <= 0)
在$scope.arrayValue
已從$simpleFactory.getArray($scope.searchWord)
獲得其價值之前執行。
所以我的問題是我怎麼能等到功能的getArray完成火我的代碼:
$scope.arrayValue = simpleFactory.getArray($scope.searchWord);
謝謝,我讀到的承諾,並設法解決這個問題:d – Erex