如何在 Aurelia 的自定义元素中设置父级 属性?
How to set a parent property from within a custom element in Aurelia?
几天前我问过这个问题
现在我需要能够在父元素 (create.js) 中重用自定义元素 (my-custom.js) 中的 allSelectableValues
。
我需要这个用于我在 create.js 上的自定义值转换器,它包含一些我需要显示名称的 ID,通过循环遍历当前获取并驻留在我的自定义元素中的元素数组.
**create.html**
<td>${d.SomeID | allSelectableValuesMapping}</td>
和
**value-converters/all-selectable-values-mapping.js**
export class AllSelectableValuesMappingValueConverter {
toView(value) {
for(let item in allSelectableValues) {
if (item.SomeID == value){
return item.Name;
}
}
}
}
在理想世界中,我希望这样的事情会奏效:
**my-custom.js**
async attached() {
this.allSelectableValues= await await this.myService.getAllValues();
this.parent.allSelectableValues = this.allSelectableValues;
}
但是我的自定义元素不知道需要它的父元素。
有没有人知道如何在自定义元素中将父元素的 allSelectableValues 设置为等于自定义元素的 allSelectableValues?或者是否有另一种更好的方法来实现它,同时仍然保持双向数据绑定自定义元素?
是这样的吗?
请特别注意 export class CustomElement
代码行上方的 @customElement('CustomElement')
声明符。
自定义元素视图模型
import {inject} from 'aurelia-framework';
import {customElement} from 'aurelia-framework';
import {bindable} from 'aurelia-framework';
@customElement('CustomElement')
export class CustomElement {
@bindable arrItems
}
自定义元素HTML
<template>
<div repeat.for="item of arrItems">$(item.someProperty}</div>
</template>
Parent 查看模型
export class ParentViewModel {
parentArrItems = [];
}
Parent HTML
<template>
<require from="customelement"></require>
<CustomElement arrItems.bind="parentArrItems"></CustomElement>
</template>
几天前我问过这个问题
现在我需要能够在父元素 (create.js) 中重用自定义元素 (my-custom.js) 中的 allSelectableValues
。
我需要这个用于我在 create.js 上的自定义值转换器,它包含一些我需要显示名称的 ID,通过循环遍历当前获取并驻留在我的自定义元素中的元素数组.
**create.html**
<td>${d.SomeID | allSelectableValuesMapping}</td>
和
**value-converters/all-selectable-values-mapping.js**
export class AllSelectableValuesMappingValueConverter {
toView(value) {
for(let item in allSelectableValues) {
if (item.SomeID == value){
return item.Name;
}
}
}
}
在理想世界中,我希望这样的事情会奏效:
**my-custom.js**
async attached() {
this.allSelectableValues= await await this.myService.getAllValues();
this.parent.allSelectableValues = this.allSelectableValues;
}
但是我的自定义元素不知道需要它的父元素。
有没有人知道如何在自定义元素中将父元素的 allSelectableValues 设置为等于自定义元素的 allSelectableValues?或者是否有另一种更好的方法来实现它,同时仍然保持双向数据绑定自定义元素?
是这样的吗?
请特别注意 export class CustomElement
代码行上方的 @customElement('CustomElement')
声明符。
自定义元素视图模型
import {inject} from 'aurelia-framework';
import {customElement} from 'aurelia-framework';
import {bindable} from 'aurelia-framework';
@customElement('CustomElement')
export class CustomElement {
@bindable arrItems
}
自定义元素HTML
<template>
<div repeat.for="item of arrItems">$(item.someProperty}</div>
</template>
Parent 查看模型
export class ParentViewModel {
parentArrItems = [];
}
Parent HTML
<template>
<require from="customelement"></require>
<CustomElement arrItems.bind="parentArrItems"></CustomElement>
</template>