【问题标题】:2 way databinding in Aurelia custom elements - bind custom element to parent viewmodelAurelia 自定义元素中的 2 路数据绑定 - 将自定义元素绑定到父视图模型
【发布时间】:2015-10-23 07:53:08
【问题描述】:

在我的应用程序中,我做了很多“服务”,我可以将它们注入到我的视图模型中以节省冗余和时间。

现在我希望更进一步,制作这些表单元素(选择、文本、复选框 - 初学者的选择下拉菜单)并将它们转换为自定义元素,仅在自定义元素中注入服务。

我可以让它在某种程度上发挥作用。当我在“父”视图中需要它时显示自定义元素(在本例中为选择),但是当我更改自定义选择元素的选定值时,它不会绑定到“父”视图模型,这是我的要求.

我希望能够通过其模板中的 bind 属性将我从自定义元素中选择的值绑定到“父”视图模型上的属性。

我会在几分钟内更新哪个小代码sn-p。

create.js(我称之为父视图模型)

import {bindable} from 'aurelia-framework';
export class Create{
    heading = 'Create';
    @bindable myCustomElementValue = 'initial value';
}

create.html(父视图)

<template>
    <require from="shared/my-custom-element"></require>
    <my-custom selectedValue.bind="myCustomElementValue"></my-custom>
    <p>The output of ${myCustomElementValue} should ideally be shown and changed here, as the select dropdown changes</p>
</template>

我的自定义.js

import { inject, bindable} from 'aurelia-framework';
import MyService from 'my-service';

@inject(MyService )
export class MyCustomCustomElement {
    @bindable selectedValue;

    constructor(myService ) {
        this.myService = myService ;
    }

    selectedValueChanged(value) {
        alert(value);
        this.selectedValue = value;
    }

    async attached() {
        this.allSelectableValues = await this.myService.getAllValues();
    }
}

最初发生的情况是 create.html 视图输出“初始值”,当我更改自定义元素选择的值时,新选择的值会被警告出来,但它不会更新绑定的父变量,即仍然只是显示“初始值”。

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    这里有几个问题:

    1. 由于不区分大小写,您需要对 DOM 中的任何属性名称进行kebab-case

      selected-value.bind="property"

      不是

      selectedValue.bind="property"

    2. 您需要双向绑定。使用装饰器创建@bindable 时,其中一个参数是BindingMode,用于设置默认绑定模式。

      Aurelia 设置了一些合理的默认值,例如input value.bind=".." 的默认值是两种方式而不是明确的

      如果您不想设置默认绑定模式,您可以明确地使用您的绑定:

      selected-value.two-way="prop"

    希望这会有所帮助:)

    编辑:我认为这个答案后 API 发生了一些变化。

    @bindable 装饰器具有以下标志:

    bindable({
      name:'myProperty',
      attribute:'my-property',
      changeHandler:'myPropertyChanged',
      defaultBindingMode: bindingMode.oneWay,
      defaultValue: undefined
    })
    

    查看快速参考的最佳位置之一是文档中的 Aurelia 备忘单:

    http://aurelia.io/docs/fundamentals/cheat-sheet

    【讨论】:

    • 哇,非常感谢。多么惊人的答案。实际上,snake-case 只是示例代码中的一个错字,但 .two-way 的作用完全不同! :D 我可以问你喜欢什么 - 装饰器还是 .two-way?
    • 您应该将默认绑定模式设置为其他人(或您自己)所期望的。在本例中,我会说默认将其设置为双向,因为这是对控件最自然的使用 - 您几乎总是希望 selected-value 绑定双向。这就是 Aurelia 使用约定的方式,而且效果很好。
    • +1 并感谢提及蛇形外壳。我没有看到文档中提到的例子是一个单词变量。当绑定与其他绑定一样完成时,疯狂地试图找出绑定是行不通的。
    • 将BindingMode 传递给@bindable 装饰器的语法是什么?我很难找到这方面的文档。它看起来像@bindable selectedValue, "two-way";,或者可能是@bindable("two-way", "selectedValue"),还是@bindable selectedValue BindingMode.twoWay;?
    • @bindable({ defaultBindingMode: bindingMode.twoWay }) 等。最好的地方是 Aurelia 文档中的备忘单:aurelia.io/docs.html#/aurelia/framework/1.0.0-beta.1.2.4/doc/…
    猜你喜欢
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多