Angularjs 角度:如何迭代不同的对象并渲染不同的模板

Angularjs 角度:如何迭代不同的对象并渲染不同的模板,angularjs,angularjs-ng-repeat,Angularjs,Angularjs Ng Repeat,假设我有: $scope.array = [{type: 'event'}, {type: 'alert'}]; 如何迭代并根据类型从$templateCache中为每个项呈现不同的项。我们可以假设'event.html'和'alert.html'存在。我认为您需要的是以下内容 <div ng-repeat='a in array'> <div ng-if="a.type =='event'"> // EVENT.html TEMPLATE

假设我有:

$scope.array = [{type: 'event'}, {type: 'alert'}];

如何迭代并根据类型从$templateCache中为每个项呈现不同的项。我们可以假设'event.html'和'alert.html'存在。

我认为您需要的是以下内容

<div ng-repeat='a in array'>
    <div ng-if="a.type =='event'">
        // EVENT.html TEMPLATE
    </div>

    <div ng-if="a.type == 'alert'">
       // ALERT.html TEMPLATE
    </div>
</div>

//EVENT.html模板
//ALERT.html模板

我想您需要的是以下内容

<div ng-repeat='a in array'>
    <div ng-if="a.type =='event'">
        // EVENT.html TEMPLATE
    </div>

    <div ng-if="a.type == 'alert'">
       // ALERT.html TEMPLATE
    </div>
</div>

//EVENT.html模板
//ALERT.html模板

除了Keegan的答案之外,您还可以做的是:

<div ng-repeat='a in array'>
    <div ng-include="a.type + '.html'"></div>
</div>


通过这种方式,您可以根据值加载event.html或error.html,除Keegan的答案外,您还可以执行以下操作:

<div ng-repeat='a in array'>
    <div ng-include="a.type + '.html'"></div>
</div>


通过这种方式,您可以根据值加载event.html或error.html,以查找不太明确且易于扩展为多种类型的解决方案。查找不太明确且易于扩展为多种类型的解决方案。假设数据来自API,这是否意味着服务器现在正在命令使用哪个模板?如果在类型和模板之间有一些中间映射,可能会更好。我无法理解。你能详细说明一下吗?可能在问题中,仅假设数据来自API,这是否意味着服务器现在正在指定要使用的模板?如果在类型和模板之间有一些中间映射,可能会更好。我无法理解。你能详细说明一下吗?可能只是这个问题