我做一個ajax(get)請求,並在角色「jobList」服務中獲得帶有json數據的承諾。
然後我用獲得的數據更新範圍。但我的問題是,更新一個範圍變量'X'我需要爲每個變量「readX」創建一個函數(見下文)。
有沒有辦法添加參數,就像在下面的代碼中的最後一個函數?
app.controller("JobListController", ['$scope', '$timeout', 'jobList',
function ($scope, $timeout, jobList) {
var readList = function (response) {
if (response) {
$timeout(function() {
$scope.list = response;
$scope.$apply();
});
}
};
var readFamilies = function (response) {
if (response) {
$timeout(function() {
$scope.allFamilies = response;
$scope.$apply();
});
}
};
var readRegions = function (response) {
if (response) {
$timeout(function() {
$scope.allRegions = response;
$scope.$apply();
});
}
};
// !!! ----- HERE ------------- !!!
var readSomething = function (response, something) {
if (response) {
$timeout(function() {
$scope[something] = response;
$scope.$apply();
});
}
};
jobList.get().then(readList);
jobList.getAll("allFamilies").then(readFamilies);
jobList.getAll("allRegions").then(readRegions);
}]);
所以需要做2個呼叫的foreach功能,設置變量,然後調用函數... – Serge
是啊,否則你可以看看封閉的方式,我編輯了我的答案 – gaurav5430
我該如何使用readSomething呢? – Serge