【发布时间】:2018-04-29 09:07:43
【问题描述】:
我试图在加载时将 ng-repeat 附加到我的 HTML div 中。 它可以识别 ng-repeat,但索引值未按应有的方式显示
HTML
<div id="myInnerCarousel"></div>
控制器:
var myCarousel = document.getElementById('myInnerCarousel');
var newItem = document.createElement("div");
newItem.setAttribute("class","item");
var newData = document.createElement("div");
newData.setAttribute("class", "col-xs-6 col-sm-6 col-md-3 col-lg-3");
newData.setAttribute("ng-repeat", "mat in " + arr);
//Create individual values seperately
var newMaterialName = document.createElement("h4");
var nameNode = document.createTextNode("{{mat.Name}}");
newMaterialName.appendChild(nameNode);
//Append everything together
newData.appendChild(newMaterialName);
newItem.appendChild(newData);
myCarousel.appendChild(newItem);
然而结果是这样的:
https://gyazo.com/00b76c6b910d4c6701059d404783f720
显示我的数组的想法是正确的,但是 angular 不能正确显示 h4。
编辑:我试图在我的 html 中实现这一点:
<div ng-controller="myController">
<div class="item">
<div class="col-xs-6 col-sm-6 col-md-3 col-lg-3" ng-repeat="mat in array1">
<h4>{{mat.Name}}</h4>
</div>
</div>
<div class="item">
<div class="col-xs-6 col-sm-6 col-md-3 col-lg-3" ng-repeat="mat in array2">
<h4>{{mat.Name}}</h4>
</div>
</div>
【问题讨论】:
-
你需要在新创建的元素上使用 Angular 的 $compile 函数。
-
我同意@RabbiShuki 的观点。对于您要解决的任何问题,这似乎都不是一种角度方法。你为什么决定像这样在页面加载时进行 DOM 操作?是否有某些原因导致您的 HTML 代码不能在您的模板文件中?
-
您对最终结果的期望有点清楚,但是您仍然没有真正描述为什么要尝试以这种非角度的方式执行此操作。 DOM Manipulation 正是 Angular 为拯救你而创建的实践,因此使用 DOM Manipulation 来创建 Angular 元素与框架设计背道而驰。
标签: javascript html angularjs appendchild