3
我一直對着我的鍵盤猛撞我的頭。AngularJs - 刪除所有基於類名的指令
我有一個指令,動態從服務器獲取數據。我根據後續的下拉列表值隱藏顯示下拉列表。我想刪除所有以前的選擇,當我做一個新的選擇。我所經歷的是,即使在我清除之後,數據仍然來自之前的選擇。
請在下面找到我的代碼: function link(scope,element)scope.displayChildren = displayChildren;
function displayChildren() {
var currentItem = scope.model;
scope.children = [];
var elementName = currentItem.name + '-child';
var elementResult = angular.element(document.querySelector('.' + elementName));
if (elementResult.length > 0) {
element.contents().remove();
elementResult.contents().remove();
element.append('<composed-components model="model"></composed-components>');
$compile(element.contents())(scope);
}
for (var i = 0, x = currentItem.children.length; i < x; i++) {
var currentChild = currentItem.children[i];
var array = currentChild.parentValue.split(',');
scope.children = getScopeChildren(array, currentChild);
}
if (scope.children.length < 1) return;
element.append('<composed-components class="{{child.parentName}}-child" ng-repeat="child in children track by child._id" model="child"></composed-components>');
$compile(element.contents())(scope);
}
function getScopeChildren(array, currentChild) {
if (scope.model.currentValue === null) {
return [];
}
if (array.length > 1) {
for (var i = 0, x = array.length; i < x; i++) {
scope.children = compare(scope.model.currentValue.value, array[i], scope.children, currentChild);
}
} else {
scope.children = compare(scope.model.currentValue.value, currentChild.parentValue, scope.children, currentChild);
}
return scope.children;
}
function compare(value1, value2, array, currentValue) {
if (value1 === value2) {
array.push(currentValue);
}
return array;
}
}
任何幫助將不勝感激。
謝謝。它沒有被刪除的原因是ng-repeat中的類留下了骯髒的html。我結束了自定義指令與一個ID的div內。這似乎正確清理。 – Michael