创建一个没有 ngRepeat 的列表

编程入门 行业动态 更新时间:2024-10-28 18:22:54
本文介绍了创建一个没有 ngRepeat 的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想创建一个不需要 ngRepeat 的指令,因为指令上有一些附加功能,与 ngRrepeat 不兼容.

这是我的 ng-repeat 指令:

<ul><li ng-repeat="item in items track by $index" ng-class="attrs.itemTheme" data-item="{{ item[attrs.itemId]}}"><div ng-include="attrs.tpl"></div>

attrs.tpl, nt-dimension 是另一个指令,它使用来自 ngRepeat 的 items 值:

没有 ngRepeat:

<ul></ul>

请给我举个例子,我已经不再为此苦苦挣扎了.

代码示例:http://jsfiddle/mato75/4zhLtjbw/

不工作的例子:http://jsfiddle/mato75/ztLhpf2g/

必须编译并附加 ngIncluded 模板,但问题是它只编译最后一个,因为摘要周期很慢.

var el = jqElm.find('ul');scope.attrs.list.forEach(function (vl) {无功 tmp ='<li class="' + attrs.itemTheme + '" data-item="' + vl.id + '">'+'<div ng-include="\'' + attrs.itemTpl + '\'"></div>'+'</li>';scope.item = vl;//这很慢:(var b = $compile(tmp)(scope);el.append(b);});

解决方案

您需要为每个 li 手动创建自己的作用域,因此每个项目都有自己的数据.

var ul = jqElm.find('ul');scope.list.forEach(函数(vl){var li = '<li><div ng-include="\'item-tpl2.html\'"></div></li>';var newScope = scope.$new();newScope.item = vl;var cLi = $compile(li)(newScope);ul.append(cLi);

I would like to create a directive, that does not need ngRepeat, because there is some additional functionality on the directive, that doesn't play good with ngRrepeat.

This is my directive with ng-repeat:

<div>
<ul>
    <li ng-repeat="item in items track by $index" ng-class="attrs.itemTheme" data-item="{{ item[attrs.itemId]}}">
        <div ng-include="attrs.tpl"></div>
    </li>
</ul>
</div>

attrs.tpl, nt-dimension is another directive, that uses the items values from ngRepeat:

<script type="text/ng-template" id="dimension-item-tpl.html">
    <div nt-dimension x-attrs="item"></div>
</script>

Without ngRepeat:

<div> 
   <ul></ul>
</div>

Can some please give me an example, I am quit struggling with this.

Example of code: http://jsfiddle/mato75/4zhLtjbw/

Not working example: http://jsfiddle/mato75/ztLhpf2g/

Got to compile and append the ngIncluded template, but the problem is, that it compiles only the last one, because the digest cycle is to slow.

var el = jqElm.find('ul');
scope.attrs.list.forEach(function (vl) {
    var tmp = 
        '<li class="' + attrs.itemTheme + '" data-item="' + vl.id + '">' +
            '<div ng-include="\'' + attrs.itemTpl + '\'"></div>' +
                '</li>';

    scope.item = vl; // this is to slow :(

    var b = $compile(tmp)(scope);
    el.append(b);
});

解决方案

You need to manually create an own scope for each li, so each item has its own data.

var ul = jqElm.find('ul');

scope.list.forEach(function (vl) {
var li = '<li><div ng-include="\'item-tpl2.html\'"></div></li>';
var newScope = scope.$new();
newScope.item = vl;
var cLi = $compile(li)(newScope);
ul.append(cLi);

这篇关于创建一个没有 ngRepeat 的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 18:07:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   列表   ngRepeat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!