【问题标题】:Aurelia binding in repeat.for not workingAurelia 在 repeat.for 中绑定不工作
【发布时间】:2016-10-13 06:33:07
【问题描述】:

我试图在 repeat.for 中的 aurelia 组件上设置一个可绑定值,但它似乎没有任何影响。

<event-summary repeat.for="event of events" event.bind="event" is-favorite="true"></event-summary>

在视图模型中

event-summary.js

@bindable('isFavorite')
@bindable('event')
export class EventSummary {
    bind(bindingContext) {
        if(bindingContext.isFavorite == null) {
            this.isFavorite = false;
        }
    }
}

事件设置正确,但无论我尝试什么,isFavorite 始终未定义 (is-favorite.bind="[some vm value]") 也返回未定义。谁能告诉我为什么?

谢谢

【问题讨论】:

    标签: javascript aurelia


    【解决方案1】:

    is-favorite.bind="true" 应该可以工作。 is-favorite="true" 也应该有效,尽管在这种情况下 isFavorite 可绑定属性将被分配字符串 'true'。这是两者的可运行示例: https://gist.run/?id=7044b0c37b53bb66e833d461f41dae2f

    【讨论】:

    • 所以它正在工作,但为什么 bindingContext 不包含 .isFavorite?它在我当前的范围内,只是对为什么它不在绑定上下文中感到困惑?
    【解决方案2】:

    我从未使用过bind() 函数,但通常你的作用域上有isFavorite,例如this.isFavorite。你也可以在构造函数中做空检查

    这对我有用:

    import {bindable} from 'aurelia-framework';
    
    export class Testelement {
    
        @bindable item
        @bindable isFavorite
        constructor() {
            console.log(this.isFavorite);
            console.log(this.item);
        }
    
        bind(bindingContext, overrideContext) {
            console.log("bc ", bindingContext); //no item or isFavorite defined here
            console.log("oc ", overrideContext);//no item or isFavorite defined here
        }
    }
    
    
    <testelement item.bind="element" repeat.for="element of data" is-favorite="true"></testelement>
    

    【讨论】:

    • 标记为答案,但我很困惑为什么 isFavorite 不在我的绑定上下文中,但它在我的本地范围内。
    猜你喜欢
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多