Angular.js中自定义占位符指令中的If语句

If statement within custom placeholder directive in Angular.js

本文关键字:If 指令 语句 占位符 js 自定义 Angular      更新时间:2023-09-26

我有个问题。

我想在模板处于编辑模式时添加一个占位符。在编辑模式下,变量viewer.building为true,否则为false。你可以在我在这里展示的代码中看到我正在尝试做什么。我需要在我的sk占位符指令属性中使用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语句只是显示为文本。有什么想法吗?当viewer.building为false时,我不能使用ng-show来隐藏整个div,因为在正常显示模式下,div中充满了一堆文本。

提前感谢!

(附言:我在树枝中使用有角度的模板,这就是为什么我使用方括号而不是花括号,这样它们就不会引起冲突)

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

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>