Given this data in my controller :

在我的控制器中给出这些数据:

$scope.data = [
    {id: "1", ElementType: "paragraph", text: "some content"},
    {id: "2", ElementType: "listItem", text: "some list item content"},
    {id: "3", ElementType: "quote", text: "some quote"}
]

An given this ng-repeat in my view:

在我看来,给出了这个ng-repeat:

<div ng-repeat="item in data">{{item.text}}</div>

How can I render this HTML ? :

如何呈现此HTML? :

<p>Some content</p>
<ul>
    <li>some list item content</li>
</ul>
<blockquote>some quote</blockquote>

I've explored ng-switch but it keeps the parent div. In other words I want to replace the HTML produced by ng-repeat with new markup based on an object attribute.

我已经探索过ng-switch,但它保留了父div。换句话说,我想用基于对象属性的新标记替换ng-repeat生成的HTML。

2 个解决方案

#1


1

You can do it inside a directive using the $compile angular function like shown in this plnkr.

您可以使用$ plangle中的$ compile angular函数在指令内执行此操作。

UPDATE Here is a plnkr that works better

更新这是一个更好的plnkr

Here is the key part:

这是关键部分:

link: function (scope, ele, attrs) {
  scope.$watch(attrs.dynamic, function(html) {

    var htmlStr = '';
    var repeat = attrs.repeat;
    for(var i = 0; i< repeat; i++){
      htmlStr += html;
    }

    ele.html(htmlStr);
    $compile(ele.contents())(scope);
  });
}

Your controller would look like this:

你的控制器看起来像这样:

function MyController($scope) {

$scope.html = '<p>{{id}}</p>\
              <ul>\
               <li>{{ElementType}}</li>\
              </ul>\
             <blockquote>{{text}}</blockquote>';
}

From your example, you'd remove the ng-repeat entirely and just use this directive. One change is that you'd pass in the whole $scope.data and dynamically pull out the bits you wanted to use in your html string. But because it's a string, that should be easy to do with simple string concatenation.

从您的示例中,您将完全删除ng-repeat并使用此指令。一个变化是你传入整个$ scope.data并动态地拉出你想在html字符串中使用的位。但是因为它是一个字符串,所以使用简单的字符串连接应该很容易。

One final thing - you are probably going to have to put some tags in so you can replace your static content with dynamic data from your array. Kind of like ng-repeat! (but different all the same).

最后一件事 - 你可能不得不放入一些标签,这样你就可以用你阵列中的动态数据替换你的静态内容。有点像ng-repeat! (但不同的都是一样的)。

Finally, you could use interpolate to fill up your html string:

最后,您可以使用interpolate来填充您的html字符串:

$interpolate(template)({
  id: id,
  ElementType: ElementType,
  text: text
});

Where you would get the data for these items from the controller

您可以从控制器获取这些项目的数据

更多相关文章

  1. 如果外部应用程序更改了持久模型(服务器数据库),AngularJS可以自
  2. 如何在rails中使用ajax将数据从控制器传递到视图
  3. $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' o
  4. Javascript来自数据库的图像上的图像映射
  5. python数据类型二(列表和元组)
  6. 将敏感数据存储在数据库中,建议
  7. sklearn进行数据预处理-归一化/标准化/正则化
  8. 如何在python中使用命名组和datadict从正则表达式模式中组合字符
  9. 使用特定顺序的ID列表从Django数据库中获取记录

随机推荐

  1. ViewFlipper+GestureDetector实现不循环
  2. Android RelativeLayout相对布局属性简析
  3. 2.6.1 使用toast显示提示信息框
  4. 安卓布局(上中下布局)
  5. Android 学习笔记
  6. android:windowSoftInputMode
  7. Android gson解析json数据工具类
  8. Android应用开发之UI组件(TextView;EditT
  9. Android 正则表达式验证手机和邮箱格式是
  10. Android 2.1 android.R.drawable Icon Re