Angular指令删除只调用一次的DOM元素
Angular Directive to delete a DOM element only called once
我希望能够删除模态中的一行。也可以添加新的。
我有一个精简的模板如下:
<div class="filter-row" delete-filter>
<div class="form-group>
<div class="delete-filter" ng-click="deleteFilter()"><i class="fa fa-trash"></i></div>
</div>
</div>
指令如下:
.directive('deleteFilter', function() {
return {
restrict: 'A',
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting");
// Remove the directive element
element.remove();
};
}
};
});
这似乎可以很好地删除单个筛选器行。但是,如果在已有行的情况下从此模板中添加新行,我只能删除其中一行。如果我添加了许多行并想要删除,我仍然只能删除一行。
注意:每次单击删除行时,无论该行是否被删除,函数中的控制台日志都会触发。
如有任何帮助,我们将不胜感激。我想我错过了什么,但我不知道是什么。
编辑:
控制器添加了一个具有此功能的新过滤器:
// Function to add a new filter row in the filter modal window
self.addFilterRow = function() {
var newFilterRow = $compile("<filter-row></filter-row>")($scope);
$("#filterForm").append(newFilterRow);
};
这是一个Plunkr,可以更好地描述我遇到的问题:
https://plnkr.co/edit/H5fvWmDLmOcnADlxELOo?p=preview
<h3>Repeat stuff:</h3>
<div ng-app='app' ng-controller="MyCtrl">
<ul>
<li ng-repeat="(country,goals) in items">{{country}}: {{goals}}
<div class="filter-row" delete-filter>
<div class="form-group>
<div class=" delete-filter " ng-click="deleteFilter() "><input type='button' value='delete'></div>
</div>
</div>
</li>
</ul>
</div>
js代码是
var items = {
"India": "2",
"England": "2",
"Brazil": "3",
"UK": "1",
"USA": "3",
"Syria": "2"
};
var app = angular.module('app', []);
app.controller('MyCtrl', function($scope) {
$scope.items = items;
});
app.directive('deleteFilter', function() {
return {
restrict: 'A',
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting");
// Remove the directive element
element.remove();
};
}
};
});
请找到你的问题:
http://jsfiddle.net/RkykR/2326/
希望它也对你有用。:)
如果我已经理解了您的问题,那么继承的作用域应该可以为您解决这个问题。将scope: true
添加到指令中,例如:
.directive('deleteFilter', function() {
return {
restrict: 'A',
scope: true,
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting");
// Remove the directive element
element.remove();
};
}
}; });
若要解决问题,请将filterRow指令的作用域设置为true。
app.directive('filterRow', function() {
return {
scope: true,
replace: true,
restrict: 'E',
templateUrl: 'filter-row.html',
};
});
app.directive('deleteFilter', function() {
return {
restrict: 'A',
scope: true,
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting",element);
// Remove the directive element
element.remove();
};
}
};
});
基本上将其设置为true将允许它创建该行的新实例。当设置为false时,该行将只有一个实例,每次单击删除时,它都会删除最后一个创建的行。
使用继承的作用域。
.directive('deleteFilter', function() {
return {
restrict: 'A',
//Use inherited scope
scope: true,
link: function(scope, element) {
// Scope function to delete a filter row from the DOM
scope.deleteFilter = function() {
console.log("Deleting");
// Remove the directive element
element.remove();
};
}
};
});
这样,ng-click="deleteFilter()"
将在deleteFilter
指令创建的作用域上调用。
更新
还要删除filterRow
指令中的replace: true
。
app.directive('filterRow', function() {
return {
scope: false,
//REMOVE this
//replace: true,
restrict: 'E',
templateUrl: 'filter-row.html',
};
});
对于同级指令上的replace: true
,deleteFilter
指令上继承的作用域将被删除。
replace:true
已弃用1
来自文档:
replace
([弃用!],将在下一个主要版本中删除,即v2.0)指定模板应替换的内容。默认为
false
。
true
-模板将替换指令的元素false
-模板将替换指令元素的内容
--AngularJS综合指令API
来自GitHub:
Caitp——它被弃用,因为
replace: true
存在已知的、非常愚蠢的问题,其中许多问题无法以合理的方式真正解决。如果你很小心,避免了这些问题,那么你就会有更多的权力,但为了新用户的利益,更容易告诉他们"这会让你头疼的,不要这样做;。
--AngularJS问题#7636
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Angular指令删除只调用一次的DOM元素
- 如何在上一次 JQuery 调用后访问 DOM
- React onClick函数在渲染到DOM时只触发一次
- 单击时将html存储到变量中,从DOM中删除,然后在另一次单击时将其放回
- 一次显示一个DOM元素的淡入/淡出列表
- 渲染速度:一次添加一个DOM项或一次添加项集
- 每10秒操作一次DOM元素
- 如何通过javascript在web应用程序中全局记录每个dom节点一次的点击
- 调用同一函数的两个dom事件如果在同一时间发生,则应该只调用该函数一次
- 添加DOM视图太慢了,需要一种更好的方法来缓存一次视图并重用它供以后使用
- 使用jquery只保存一次自定义数据到dom元素
- 如何让一个javascript函数只执行一次dom就绪
- ng2 初始化 DOM 属性一次并删除更改检测
- 一次在DOM中维护5个部分
- Javascript和DOM - insertBefore只执行一次
- 如果我在Javascript事件处理程序中对DOM进行更改,这些更改是增量呈现,还是只在最后呈现一次