简体   繁体   English

AngularJS和DOM Elements注入

[英]AngularJS and DOM Elements injections

Please forgive me for the title, I didn't know exactly how to describe it. 请原谅我的标题,我不知道该怎么形容。

I'm new to AngularJS and I wanted to ask for your advice on how to create something. 我是AngularJS的新手,我想请教您有关如何创建内容的建议。

I've a list that I'm drawing with ngRepeat. 我有一个要用ngRepeat绘制的列表。 When you click on each list item, a collapsing menu opens. 当您单击每个列表项时,将打开一个折叠菜单。 This menu is the same for all elements of the list. 该菜单对于列表的所有元素都是相同的。 At the moment, my ngRepeat draws this menu to each and every list item and shows it only if the corresponding list item was clicked. 此刻,我的ngRepeat会在每个列表项上绘制此菜单,并仅在单击相应的列表项时才显示该菜单。 So if my list has 50 items, I'm drawing the menu 50 times! 因此,如果我的列表中有50个项目,那么我将绘制菜单50次! This is 49 times more than I really need. 这是我真正需要的49倍。

If I was to do this with jQuery, I'd just create the menu and change its location on the dom. 如果要使用jQuery进行此操作,则只需创建菜单并更改其在dom上的位置。 How can I do this the Angular way? 我该如何以Angular方式进行操作?

Example image of how the list looks like, the gray item is the menu with its action. 列表外观的示例图像,灰色项目是带有其操作的菜单。 Looks the same for all items and call the same functions but with different parameters: http://i.stack.imgur.com/rvk5L.jpg 所有项目看起来都一样,并调用相同的函数,但参数不同: http : //i.stack.imgur.com/rvk5L.jpg

I guess in these type of cases ng-if will be a better option as this will create the dom based on the condition rather than showing/hiding already created elements as in the case of ng-show . 我猜在这种情况下ng-if将是一个更好的选择,因为它将根据条件创建dom,而不是像ng-show那样显示/隐藏已创建的元素。

From docs: 从文档:

The ngIf directive removes or recreates a portion of the DOM tree based on an {expression}. ngIf指令会根据{expression}移除或重新建立DOM树的一部分。 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. 如果分配给ngIf的表达式的值为假,则将元素从DOM中删除,否则将元素的克隆重新插入DOM中。

Here is a demo: http://plnkr.co/edit/Ro9LOB4f81LT32mGMyij?p=preview 这是一个演示: http : //plnkr.co/edit/Ro9LOB4f81LT32mGMyij?p=preview

单击菜单项时使用ngIf而不是ngShow ,在这种情况下html根本不会呈现(当然,当表达式为false时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM