3

抱歉,我无法提供有关此问题的任何代码片段,我是AngularJS的新手。

<div ng-repeat="item in list" ng-view></div>

使用上面的代码,是否可以渲染依赖于item.type属性的不同模板。我期待这样的结果:

  • item.type == "image"返回: <div><img src="'IMAGE_URI'"></div>
  • item.type == "text"返回: <div><p>TEXT</p></div>

到目前为止,我已经为枚举创建了一个模板 html item.type。使用AngularJS可以解决这个问题吗?我最近了解到,ng-view伴随着ng-route.

4

2 回答 2

4

我认为你可以做到的一种方法是使用'ng-if'有条件地包含html:

 <div ng-repeat="item in list">
      <div ng-if="item.type == 'image'><img src="'IMAGE_URI'"></div>
      <div ng-if="item.type == 'text'><div><p>TEXT</p></div>
 </div>
于 2014-05-21T12:32:43.853 回答
3

你只能有一个 ng-view,
看看这个答案

来自 ng-view 的文档

ngView is a directive that complements 
the $route service by including the rendered
template of the current route into the main
layout (index.html) file.

Every time the current route changes,
the included view changes with it according 
to the configuration of the $route service.

Requires the ngRoute module to be installed.

您正在寻找的是ng-include,结合ng-switch,看看这个答案如何结合两者。

ng-include 创建一个新的子作用域,该作用域又继承自控制器。查看此答案以获取有关该主题的更多信息。

于 2014-05-21T07:45:27.987 回答