【问题标题】:If statement within custom placeholder directive in Angular.jsAngular.js 中自定义占位符指令中的 If 语句
【发布时间】:2014-09-24 22:49:32
【问题描述】:

我有一个问题。

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

<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 使用 ng-show 为 false 时,我不能只隐藏整个 div,因为在正常显示模式下 div 被一堆文本填满。

提前致谢!

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

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

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

    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>
    

    【讨论】:

    • i.imgur.com/iusDR4X.png 条件被忽略。全文被设置为内容。尝试使用和不使用 else 语句。
    • 你试过这个sk-placeholder="[[viewer.building &amp;&amp; 'Summarise the content of your course by writing a short blurb. You can add a more elaborate description on the next page.' || '']]"吗?
    • 很好,就像一个魅力。我已经用过滤器让它工作了,但是用你建议的代替它。现在我有另一个问题,当页面呈现时,占位符被放入 div 中。通过单击按钮从编辑更改为学生视图,占位符属性变为空,但文本仍保留在 div 内。
    • div内文不是来自viewer.course.description吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2011-10-14
    • 2019-05-22
    • 1970-01-01
    • 2015-02-08
    相关资源
    最近更新 更多