简体   繁体   English

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

[英]If statement within custom placeholder directive in Angular.js

I have a question. 我有个问题。

I want to add a placeholder when the template is in edit mode. 我想在模板处于编辑模式时添加一个占位符。 When in edit mode, the variable viewer.building is true, otherwise false. 在编辑模式下,变量viewer.building为true,否则为false。 You can see in the code I'm showing here what I'm trying to do. 您可以在此处显示的代码中看到我要执行的操作。 I need an if-statement within my sk-placeholder directive attribute. 我的sk-placeholder指令属性中需要一个if语句。 If viewer.building is true, there's text. 如果viewer.building为true,则显示文本。 Otherwise there is an empty placeholder attribute. 否则,将有一个空的占位符属性。

<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>

Unfortunately the if statement is just showing up as text. 不幸的是,if语句只是显示为文本。 Any ideas? 有任何想法吗? I can't just hide the whole div when viewer.building is false using ng-show because the div is being filled up with a bunch of text in normal display mode. 当使用ng-show的viewer.building为false时,我不能仅隐藏整个div,因为在正常显示模式下div填充了一堆文本。

Thanks in advance! 提前致谢!

(PS : I'm using angular in twig templates, that's why I'm using square brackets instead of curly ones so they don't cause conflicts) (PS:我在树枝模板中使用了尖角,这就是为什么我使用方括号而不是大括号,以便它们不会引起冲突)

Try using a conditional expression in the following format: 尝试使用以下格式的条件表达式:

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

In your case it should look something like this: 在您的情况下,它应如下所示:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM