我有一個功能,讓我們說:
$scope.addNode = function (param) {
//this is a function to add a child to a tree view node, sent via the param argument
var newNode = {
//add the different properties I need for the new node
};
if(param.hasOwnProperty('children') && param.children != null) {
param.children.push(newNode);
}
else {
param.children = [];
param.children.push(newNode);
}
$scope.$apply(); // calling $apply because I need the newNode to be rendered
$scope.setFocusedNode(newNode); //highlight the new node, change attributes, etc
$scope.editNodeText(newNode); //call inline Editing for the new node, which also involves DOM manipulation; this is basically where everything fails because without the apply, the DOM element for the newNode doesn't exist.
}
我使用同樣的功能從一個jQuery keyup事件,並從NG點擊指令。 該代碼從keyup事件工作正常,但從指令調用時,我得到一個「$應用已在進行中」的錯誤,因爲ng-click已經做了天生的$應用。
但是,刪除$ apply也不起作用,因爲我需要爲它後面的代碼更新範圍AND我不能用普通的onclick替換ng-click,因爲click函數也是該範圍內的對象可以更改。
有沒有辦法在沒有得到「$ apply already in progress」錯誤的情況下說「刷新範圍」?請注意,即使我得到的錯誤,範圍得到更新corectly和工作好,即使從ng點擊調用(除了IE只是扼流圈和javascript停止工作)
你可以考慮這個https://coderwall.com/p/ngisma –
@Joel_Blum我已經試過,但這不是我要找的。即使當我從ng-click(因此,在一個隱式應用中)調用函數時,我仍然需要執行$ apply,因爲我需要爲函數的下一部分更新範圍。 – Bogdan
你能再提供一些代碼嗎?在申請之前和之後你做了什麼?我認爲你需要找到一個更清潔的解決方案... –