[英]Mutable variable is accessible from closure inside angular services
我正在使用angular ui網格庫,並嘗試填充自定義下拉過濾器。 該過濾器只能應用於特定的列,這就是為什么我編寫了for循環以動態訪問列的原因。 對於每個特定的列,我通過我的角度服務向api發送一個請求,以獲取過濾器的值,如果響應成功,則將每個列的屬性ID與返回的數據ID進行比較,然后填充過濾器值。
內部出現的問題then
功能,IDE顯示我的警告
可從閉包訪問可變變量
和內部then
起作用沒有什么作品。
我在這里閱讀了多個主題,發現我必須執行自調用功能,但是它不能正常運行。
那我的錯誤在哪里? 提前致謝
碼
for (var i = 0; i < $scope.columns.length; i++) {
// ===== rebuild columns tree =====
var execute_ids = [];
angular.forEach($scope.columns[i].property, function(){
if ($scope.columns[i].property.strict === true){
if(execute_ids.indexOf($scope.columns[i].property.propertyTypeId) === -1){
execute_ids.push($scope.columns[i].property.propertyTypeId);
lovServices.customPropFilterValue(execute_ids)
.then(function (response) {
(function () {
if (response.id == $scope.columns[i].property.propertyTypeId){
$scope.filter.push({
value: data.value,
label: data.value
});
$scope.columns[i].filter = {
type: uiGridConstants.filter.SELECT,
condition: uiGridConstants.filter.EXACT,
selectOptions: $scope.filter
};
}
})()
});
}
}
});
}
代碼的原始部分
lovServices.customPropFilterValue(execute_ids)
.then(function (response) {
if (response.id == $scope.columns[i].property.propertyTypeId){
$scope.filter.push({
value: response.value,
label: response.value
});
$scope.columns[i].filter = {
type: uiGridConstants.filter.SELECT,
condition: uiGridConstants.filter.EXACT,
selectOptions: $scope.filter
};
}
});
編輯
我使用了@JuanTonina提供的解決方案,但警告已消失,但似乎又出現了一個問題。 then
函數中的i
返回錯誤值
(function (i) { /* note that the lovServices call is INSIDE the IIFE*/
console.log($scope.columns[i].property.propertyTypeId)
// this console log returns correct ids (120, 123, 194)
lovServices.customPropFilterValue(execute_ids)
.then(function (response) {
console.log($scope.columns[i].property.propertyTypeId)
// this console log returns wrong ids (120, undefined, 114)
if (response.id == $scope.columns[i].property.propertyTypeId) {
$scope.filter.push({
value: data.value,
label: data.value
});
$scope.columns[i].filter = {
type: uiGridConstants.filter.SELECT,
condition: uiGridConstants.filter.EXACT,
selectOptions: $scope.filter
};
}
});
})(i)
我將其添加為答案,因為評論不足以顯示我的意思。
您的代碼應如下所示:
for (var i = 0; i < $scope.columns.length; i++) {
// ===== rebuild columns tree =====
var execute_ids = [];
angular.forEach($scope.columns[i].property, function () {
if ($scope.columns[i].property.strict === true) {
if (execute_ids.indexOf($scope.columns[i].property.propertyTypeId) === -1) {
execute_ids.push($scope.columns[i].property.propertyTypeId);
(function (i) { /* note that the lovServices call is INSIDE the IIFE*/
lovServices.customPropFilterValue(execute_ids)
.then(function (response) {
if (response.id == $scope.columns[i].property.propertyTypeId) {
$scope.filter.push({
value: data.value,
label: data.value
});
$scope.columns[i].filter = {
type: uiGridConstants.filter.SELECT,
condition: uiGridConstants.filter.EXACT,
selectOptions: $scope.filter
};
}
});
})(i)
}
}
});
}
這樣做的原因是,您的變量i
在用於回調之前正在更改。 附帶說明一下,如果您使用的是es6,那么for ( let i = 0;...)
也可以解決此問題
由於您有立即調用的函數表達式(簡稱IIFE),因此可能導致此問題。 並且它在創建后立即執行。
從您的.then()
方法內部刪除,它應該可以正常工作。
(function () {
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.