简体   繁体   English

淘汰赛绑定字体类

[英]Knockout binding fontawesome class

Exmaple of posted code 张贴代码的范例

Hello, 你好,

I have a problem with binding CSS class of font-awesome into an tag. 我将font-awesome的CSS类绑定到标签中时遇到问题。

HTML: HTML:

    <div class="flex-no-shrink">
        <div class="btn-group">
            <div data-bind="foreach: toolbarButtons">
                <button type="button" class="btn btn-xs btn-primary" data-bind="enable: enabled, visible: visible, click: onClick">
                   <i class="fa" data-bind="css: { class: icon }"></i>
                    <!-- <i class="fa fa-plus"></i> -->
                </button>
                  <button type="button" class="btn btn-xs btn-primary" data-bind="enable: enabled, visible: visible, click: onClick">
                     <i class="fa fa-plus"></i>
                </button>
            </div>
        </div>
    </div>

TS: TS:

interface IToolbarButton {
    enabled: KnockoutComputed<boolean>;
    visible: KnockoutComputed<boolean>;
    icon: string;
    onClick();
}

export abstract class ViewModel {
       toolbarButtons: IToolbarButton[];

   constructor(){
   this.loadDefaultToolbar();
   }
   loadDefaultToolbar(): void {
        this.toolbarButtons = [];
        //add button
        this.toolbarButtons.push({
            enabled: knockout.pureComputed(() => true/*some internal logic*/),
            icon: 'fa fa-plus',//this is what I want to place inside <i> tag
            onClick: () => {/*some internal logic*/},
            visible: knockout.pureComputed(() => true/*some internal logic*/)
        });
        //other default buttons...
        }
};

ko.applyBindings(new ViewModel());

What is a correct way to bind in mine situation? 束缚地雷的正确方法是什么?

Exmaple of posted code 张贴代码的范例

I tried base binding like text or just css: { icon } but they now work also. 我尝试了诸如文本CSS的基本绑定:{icon},但它们现在也可以使用。

Thank you for your time and help 😉 谢谢您的时间和帮助😉

The css binding has two forms: css绑定有两种形式:

  1. One which accepts an object with classes as property names and values as boolean expressions: 接受一个对象,该对象的类为属性名,值作为布尔表达式:

     css: {className: booleanExpression} 

    ...where className is the name of the class to include if booleanExpression is true (it will be left out if booleanExpression is false). ...其中className是如果booleanExpression为true时要包括的类的名称(如果booleanExpression为false则将被忽略)。

  2. One which lets you specify the class name(s) to include as a string: 一种可以让您指定要包含为字符串的类名称:

     css: stringExpression 

You've sort of tried to combine the two syntaxes; 您已经尝试过将两种语法结合起来。 you want the second one. 你想要第二个。 Unlike (mis)using the attr binding, this respects other classes added to the element. 与(错误地)使用attr绑定不同,这尊重添加到元素中的其他类。 This feature of the css binding is documented here and can be found in the source here . css绑定的此功能在此处记录 ,可以在此处源代码中找到

Live Example: 现场示例:

 ko.applyBindings({ someClassName: "two", }) 
 .one { background: yellow; } .two { color: red; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <div class="one" data-bind="css: someClassName"> My type is red because the view model added a class name! </div> 

If you'd like to see/compare more examples, please leave a comment. 如果您想查看/比较更多示例,请发表评论。

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

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