我试图取决于一个范围变量的DOM元素将改变类型实现..根据AngularJS变量改变DOM元素类型
所以说我有一个像范围VAR:
$scope.post = {
title: 'Post title goes here!',
slug: 'post-title-goes-here',
category: 'News',
featured_image: '//src.jpg'
};
和DOM元素,如:
<a href="#/post/{{post.slug}}">
<div style="background-image: url({{post.featured_image}});">
<small ng-if="post.category">{{post.category}}</small>
<h3>{{post.title}}</h3>
</div>
</a>
但是,如果VAR $scope.post.slug
是空的DOM元素看起来像:
<div>
<div style="background-image: url({{post.featured_image}});">
<small ng-if="post.category">{{post.category}}</small>
<h3>{{post.title}}</h3>
</div>
</div>
很明显,我可以做一个ng-if
并复制每一点代码,但我试图避免这一点!
像这样的理想:
<{{post.slug ? 'a href="#/post/'+post.slug+'"' : 'div'}}>
<div style="background-image: url({{post.featured_image}});">
<small ng-if="post.category">{{post.category}}</small>
<h3>{{post.title}}</h3>
</div>
</{{post.slug ? 'a' : 'div'}}>
的条件是两个'NG-if's相同... –
你”再右吧。修正并更新了这一点。我正在用几种不同的方式来做这个检查。 OP可以编辑这些值以匹配他拥有的数据。 – AndrewR