【发布时间】:2017-01-31 07:34:44
【问题描述】:
我想要实现的是取决于 DOM 元素将改变类型的范围变量..
所以说我有一个范围变量,例如:
$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'}}>
【问题讨论】:
标签: javascript jquery angularjs dom