Ember.js 删除Ember组件中的空属性绑定
我正在尝试为新项目中的可单击图标制作一个余烬组件(在过程中了解余烬),但遇到了属性绑定问题: 如何从呈现的HTML中删除空属性绑定 根据官方指南,我可以绑定到字符串或数字以使属性与字符串/数字一起弹出,或者绑定到布尔值以切换属性(假设属性和类名遵循相同的结构),但是如果我传递布尔值false以删除属性,则其值仅设置为字符串“false” 部件代码Ember.js 删除Ember组件中的空属性绑定,ember.js,Ember.js,我正在尝试为新项目中的可单击图标制作一个余烬组件(在过程中了解余烬),但遇到了属性绑定问题: 如何从呈现的HTML中删除空属性绑定 根据官方指南,我可以绑定到字符串或数字以使属性与字符串/数字一起弹出,或者绑定到布尔值以切换属性(假设属性和类名遵循相同的结构),但是如果我传递布尔值false以删除属性,则其值仅设置为字符串“false” 部件代码 export default Ember.Component.extend({ tagName: 'i', classNameBindings
export default Ember.Component.extend({
tagName: 'i',
classNameBindings: ['icon'],
attributeBindings: ['title', 'roleAttribute:role'],
icon: 'blank', // Defaults to a blank icon
title: '', // Defualts to empty title
click() {
this.sendAction();
},
roleAttribute: function() {
return this.get('action') ? 'button' : false;
}.property()
});
{{ui-icon icon="close" title="Close" action="hide"}}
{{ui-icon icon="close" title="Close"}}
模板中的用法
export default Ember.Component.extend({
tagName: 'i',
classNameBindings: ['icon'],
attributeBindings: ['title', 'roleAttribute:role'],
icon: 'blank', // Defaults to a blank icon
title: '', // Defualts to empty title
click() {
this.sendAction();
},
roleAttribute: function() {
return this.get('action') ? 'button' : false;
}.property()
});
{{ui-icon icon="close" title="Close" action="hide"}}
{{ui-icon icon="close" title="Close"}}
预期结果(为了清晰起见,删除了成员ID和类名)
真实结果(为了清晰起见,删除了成员ID和类名)
如果我传递一个空字符串而不是false,我仍然会得到角色,但没有分配值,但我根本不想要角色。基于问题线程中的,您应该返回null
而不是false
,以删除属性
我已经更新了《余烬指南》,其中包括: