【问题标题】:Returning specific values from JS switch case with knockout bindings从带有淘汰绑定的 JS 开关盒返回特定值
【发布时间】:2018-08-15 13:02:30
【问题描述】:

所以我有一个 switch case 函数,它返回多个选项,我试图在各种敲除绑定中引用每个选项,但是它不起作用?

function messageIcon (type) {
    return ko.computed(function () {
        switch (type) {
            case InteractionMessageTypes.Customer:
                return {
                    tooltip: "This message was sent by the customer",
                    icon: "icon icon-user4",
                    contentClass: "customer-message"
                };
            case InteractionMessageTypes.Agent:
                return {
                    tooltip: "This message was sent by an agent",
                    icon: "icon icon-headset",
                    contentClass: "agent-message"
                };
            case InteractionMessageTypes.Initiate:
                return {
                    tooltip: "This session has started",
                    icon: "icon icon-phone2",
                    contentClass: "system-message"
                };

<div data-bind="css: messageIcon($data.contentClass)">
  <span data-bind="css: messageIcon($data.icon), attr:{title: messageIcon($data.tooltip)}"></span>
</div>

【问题讨论】:

    标签: javascript switch-statement knockout-2.0


    【解决方案1】:

    解决这个问题的方法是将每种情况下的回报从

     return {
                        tooltip: "This message was sent by the customer",
                        icon: "icon icon-user4",
                        contentClass: "customer-message"
                    };
    

    return '<span class="icon icon-user customer-message" title="This message was sent by the customer">' + '</span>';
        }
    

    然后像这样对html进行数据绑定

     <span data-bind="html: messageIcon($data.Type)">
    
                    </span>
    

    【讨论】:

      猜你喜欢
      • 2013-08-14
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2014-03-30
      相关资源
      最近更新 更多