angular - 如何在跨度Angular2 +中绑定占位符
问题描述
我的情况我只想在跨度中绑定一个占位符。当我使用输入时它工作得很好,但我必须在可编辑的范围内进行
<span
#newComment
class="postcomment"
contenteditable="true"
[placeholder]="'Comment.Write'|translate"
// it's working if I don't bind it like this : placeholder="your comment"
>
</span>
我的错误是:
Can't bind to 'placeholder' since it isn't a known property of 'span'
如果您有解决方案,谢谢。而且我不能使用输入:)
解决方案
正如评论者已经明确指出的那样,该属性对元素placeholder
无效,即使具有该属性也是如此。<span>
contenteditable
当您设置时contenteditable
,在您设置的代码中,innerHTML
或者innerText
如果数据为空白,您可以注入一个占位符模板。然后,您需要绑定到焦点事件并在焦点输入和输出时清除/重新添加此模板。
如果您经常使用它,将它包装在指令中最适合您。一个简短的例子;
组件.ts 代码
comment = null; // When comment is null, the template will be shown
template = 'Please enter your comment...';
组件.html代码
<span
#newComment
(focusin)="comment = (comment === null ? '' : comment)"
(focusout)="newComment.innerText.trim() === '' ? comment = null : comment = newComment.innerHTML"
[innerHTML]="comment === null ? template : comment"
contenteditable="true">
</span>
推荐阅读
- google-bigquery - 日期的滞后函数
- flutter - 是否可以通过在颤振中使用 Provider 来跨页面更新值?
- c - 数组上的逻辑左移和逻辑右移
- java - spring 3.x 是否支持@JmsListener 或@RabbitListener 注解?
- google-apps-script - Appscript Webapp 超链接不起作用 - 尝试导航顶级窗口的框架已被沙盒化
- database - 如何动态制作分区?
- c# - 使用基于 cookie 的身份验证的所有设备策略的注销是什么?
- tarantool - box.info.vclock 中的旧实例
- asp.net-core - IdentityServer4 response_mode web_message
- airflow - 气流从第一个任务开始