我正在開發一個使用AngularJS的應用程序。在我的應用程序的某個部分,我有一個按鈕,當我單擊該按鈕時,它會添加一個新的輸入來添加電話。對於每個輸入,都有一個刪除鏈接來刪除該輸入。問題與angular.element使用計數器的元素
用我的代碼,我使用計數器。
由於某些原因,我無法弄清楚它!
查看:
<div id="div{{$index}}" style='z-index: 1' ng-repeat="cEdit in telephone" ng-show="contactsElements">
<input type="text" ng-model="cEdit" maxlength="15">
<a href="javascript:void(0)" ng-click="removeDIV($index)">Remove</a>
</div>
<div ng-show="contactsElements">
<div id="space-for-new-telephones"></div>
<button ng-click="addDIV()">Add</button>
</div>
JS:
$scope.removeDIV = function (arrAddr) {
console.log("Array Index = " + arrAddr);
var iEl = angular.element(document.querySelector('#div' + arrAddr));
iEl.remove();
if ($scope.count > 0) {
$scope.count = $scope.count - 1;
console.log($scope.count);
}
}
$scope.addDIV = function() {
if ($scope.count <= 2) {
angular.element(document.getElementById('space-for-new-telephones')).append("<div id='div" + $scope.count + "'><input type='text' ng-model='$scope.telephone.num[" + $scope.count + "]' maxlength='15'> <a href='javascript:void(0)' ng-click='removeDIV(" + $scope.count + ")'>Remove</a></div>");
$scope.count++;
console.log($scope.count);
}
}
我的問題是我手動按下按鈕,將它們添加輸入......它看起來像我有追加一個問題命令。
ng-click='removeDIV(" + $scope.count + ")'
我能夠添加元素並將其顯示在視圖中,但以後無法將其刪除。
請注意,已經在視圖中使用ng-repeat的元素正常工作,我可以刪除它們。
有什麼區別?它和我的代碼一樣。此外,除非我使用指令,否則此代碼不起作用。 –
我想如果你添加$ compile作爲依賴項,它將在控制器上工作 –
謝謝阿布蘇菲,我會試試這個,如果它能工作,我會盡快給你回覆 –