我正在通過 Chrome 控制臺操作一些角度服務/功能。 (我必須專門為我正在處理的任務執行此操作)。
我想要做的是等待AddBagIfLimitNotReached()函式執行并完成運行。然后才訪問變數this.option.Quantity。
angular.element(document.querySelector(".quantity-button")).controller()._registeredControls[1].Scope.AddBagIfLimitNotReached = async function(n) {
console.log("tthis", this)
if (this.HasReachedMaximumBaggageAllowance()) {
angular.element(document.querySelector(".quantity-button")).controller()._registeredControls[1].LuggageDrawersService.OpenLuggageLimitReachedDrawer();
return;
}
this.AddBag(n);
console.log("Quantity", this.option.Quantity);
};
使用此功能,我將產品添加到我的籃子中。并且this.option.Quantity應該 console.log 1。但它實際上是 consoles.log 0。
但是,如果我檢查物件本身,它會顯示1.
所以我想正在發生的事情是,我正在控制臺.記錄我的行李數量,在行李真正完成添加到籃子之前。
例如,如果我添加了settimeout2 秒的 a,則正確的包值 = 1 是 console.logged。
angular.element(document.querySelector(".quantity-button")).controller()._registeredControls[1].Scope.AddBagIfLimitNotReached = async function(n) {
console.log("tthis", this)
if (this.HasReachedMaximumBaggageAllowance()) {
angular.element(document.querySelector(".quantity-button")).controller()._registeredControls[1].LuggageDrawersService.OpenLuggageLimitReachedDrawer();
return;
}
this.AddBag(n);
// Returns 1
setTimeout(function(){ console.log("Quantity", this.option.Quantity); }, 2000);
};
有沒有更好的方法可以在不使用 settimeout 的情況下實作這一目標?我嘗試過 async/await/promises,但我似乎仍然找不到等待函式完成加載的方法。
Async/await 回傳一個錯誤 - 它不喜歡該函式this.HasReachedMaximumBaggageAllowance()并拋出一個錯誤說明this.HasReachedMaximumBaggageAllowance is not a function.
任何提示/想法將不勝感激。
uj5u.com熱心網友回復:
我找到了一個解決方案,我正在使用$watch, 來觀察this物件中的鍵/值。這似乎有效:
angular.element(document.querySelector(".quantity-button.plus-button")).controller()._registeredControls[1].Scope.AddBagIfLimitNotReached = function(n) {
let bagCount = this.option.Quantity;
console.log("bagCount", bagCount);
if (this.HasReachedMaximumBaggageAllowance()) {
angular.element(document.querySelector(".quantity-button.plus-button")).controller()._registeredControls[1].LuggageDrawersService.OpenLuggageLimitReachedDrawer();
return;
};
this.AddBag(n);
this.$watch("this.option.Quantity", function (newValue) {
console.log(`Value of foo changed ${newValue}`);
if (newValue > 0) {
document.querySelector(`.luggage-tile-weight-${n.Weight} .tile-title .tick-box`).classList.add("green-tick");
displayGreenTickNoBagSelected();
};
if (newValue === 0) {
document.querySelector(`.luggage-tile-weight-${n.Weight} .tile-title .tick-box`).classList.remove("green-tick");
displayGreenTickNoBagSelected();
};
});
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361596.html
上一篇:離子造型容器
