angularjs - 为什么 $compile 在 ng-repeat 中创建多个元素?
问题描述
为什么 $compile 在 ng-repeat 中创建多个元素?
HTML:
<li ng-repeat="action in actions" set-on-click function="{{action.function}}"><i class="{{action.class}}" aria-hidden="true"></i>{{action.text}}</li>
指示:
directive('setOnClick', ['$compile', function ($compile) {
return {
restrict: "A",
link: function(scope, elm, attrs, ctrl)
{
console.log(attrs.function);
elm.attr("ng-click", attrs.function);
elm.removeAttr("set-on-click");
$compile(elm)(scope);
}
};
}]);
$scope.actions
我从有 3 条记录的数据库中获取价值。我必须使用指令才能绑定ng-click
到我从数据库中获取的函数。而不是得到 3 li 项目,我得到 9。每个 li 都重复三次。有人可以解释为什么吗?有没有办法阻止这种重复?
解决方案
推荐阅读
- swift - Flickr 照片上传不适用于 OAtuth1Swift postImage 方法
- java - 从java中的项目文件夹(不是bin)加载图像
- c++ - Compound assignment E1 op= E2 is not equivalent to E1 = E1 op E2
- c# - 如何将解析存储的字符串从数据库反转为浏览器理解的 HTML 标记?
- pandas - 熊猫逐列删除重复值
- ionic-framework - 如何从科尔多瓦离子应用程序中的一个信号附加数据对象访问数据
- java - RecyclerView Android 不显示 Gson 响应的任何结果
- php - 如何从php中的对象获取国家代码?
- lua - 为什么将一个表作为值分配给另一个表会导致问题?
- fortran - 用 MVAPICH2 编译 Fortran 2003 程序