javascript - AngularJS指令: How do I dynamically change the element html in an ng-repeat?

标签 javascript html angularjs angularjs-directive

我有一个 Angular Directive(指令),我正在对其应用 ng-repeat。该指令的目的是将自身替换为基于传递到原始指令的值的不同指令:

<content-type-directive type="item.type" ng-repeat="item in items"></content-type-directive>

变成了

<type1-specific-directive></type1-specific-directive>
<type2-specific-directive></type2-specific-directive>
<type1-specific-directive></type1-specific-directive>
<type1-specific-directive></type1-specific-directive>
...

完整示例:http://jsfiddle.net/qPGZ8/6/

如果第二个指令的 html 可以在第一个指令的包装元素内呈现,那么这将是直接执行的,但在这种情况下,我需要它来实际替换原始项目。

这与 Customizing the template within a Directive 相关

在 Misko 的回答中,他解释了使用 element.replaceWith() 来交换原始 html。当该项目不在 ng-repeat 中时,此方法可以正常工作,但在 ng-repeat 内部时,它会破坏 ng-repeat 使用新元素更新自身的能力。

最佳答案

与错误 https://github.com/angular/angular.js/issues/2151 有关。升级到最新版本 1.2.0 即可修复该问题。

顺便说一句,按钮应该放在 Controller 的范围内。希望这是一个错字。

Demo

关于javascript - AngularJS指令: How do I dynamically change the element html in an ng-repeat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18835579/

相关文章:

angularjs - 无法直接从浏览器访问 Angular 组件

javascript - 在没有数字占位符的情况下在 JavaScript 中复制 C# 的 string.format()

javascript - 页面上加载了超过 100 个 soundcloud iframe 小部件

javascript - 使用 2D 渲染上下文的 HTML5 Canvas 转换问题

javascript - 如何引导在友好的 IFRAME 中加载的 AngularJS 应用程序

javascript - 如何使用数组 (javascript) 将图像链接到下拉列表值?

html - Asp .Net 客户端动态文本

javascript - 关于 JavaScript getCookie 函数的澄清?

javascript - 我想使用 Xrm.WebApi.online.executeMultiple 从 Dynamics 中删除多个 CRM 记录

html - 使用 CSS 将多个图像 (.gif) 用作图标,而不是背景,插入到不同的行中?