【发布时间】: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