如果语句在Angular.js中的自定义占位符指令内

时间:2014-09-24 13:46:32

标签: javascript angularjs

我有一个问题。

我想在模板处于编辑模式时添加占位符。在编辑模式下,变量viewer.building为true,否则为false。您可以在代码中看到我在这里展示我正在尝试做什么。我需要在sk-placeholder指令属性中使用if语句。如果viewer.building为true,则为文本。否则,会有一个空的占位符属性。

<div contentEditable="[[viewer.building]]" ng-model="viewer.course.description" 
sk-placeholder="[[viewer.building]] ? 'Summarise the content of your course by 
writing a short blurb. You can add a more elaborate description on the next 
page.'"></div>

不幸的是,if语句只是显示为文本。有任何想法吗?我不能只使用ng-show隐藏整个div,因为div在正常显示模式下被填充了一堆文本。

提前致谢!

(P.S。:我在树枝模板中使用角度,这就是为什么我使用方括号而不是卷曲的,所以它们不会引起冲突)

1 个答案:

答案 0 :(得分:0)

尝试使用以下格式的条件表达式:

attribute="condition && if-true || if-false"

在你的情况下它应该是这样的:

<div contentEditable="[[viewer.building]]" ng-model="viewer.course.description" 
sk-placeholder="[[viewer.building && 'Summarise the content of your course by 
writing a short blurb. You can add a more elaborate description on the next 
page.' || '']]"></div>