请原谅我的标题,我不知道如何准确描述它。
我是 AngularJS 的新手,我想征求你关于如何创建一些东西的建议。
我有一个用 ngRepeat 绘制的列表。当您单击每个列表项时,将打开一个折叠菜单。该菜单对于列表中的所有元素都是相同的。目前,我的 ngRepeat 将此菜单绘制到每个列表项,并且仅在单击相应的列表项时才显示它。因此,如果我的列表有 50 个项目,我就会绘制菜单 50 次!这比我真正需要的多了 49 倍。
如果我使用 jQuery 来完成此操作,我只需创建菜单并更改其在 dom 上的位置。我怎样才能以 Angular 的方式做到这一点?
列表外观的示例图像,灰色项目是带有其操作的菜单。所有项目看起来都相同,并调用相同的函数但具有不同的参数: /image/rvk5L.jpg
最佳答案
我想在这种情况下 ng-if
将是一个更好的选择,因为这将根据条件创建 dom,而不是像 那样显示/隐藏已经创建的元素>ng-show
.
来自文档:
The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. If the expression assigned to ngIf evaluates to a false value then the element is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.
关于javascript - AngularJS 和 DOM 元素注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114787/