gpt4 book ai didi

javascript - Aurelia 双向绑定(bind)无法正常工作

转载 作者:行者123 更新时间:2023-11-28 18:32:45 25 4
gpt4 key购买 nike

我正在尝试在 Aurelia 中进行双向绑定(bind),但似乎无法使其正常工作。

所以我有create.html,其中有 selectedTimeZone.two-way="timeZone" 。我试图通过执行 <div if.bind="timeZone">Main: ${timeZone}</div> 来显示它正在工作和绑定(bind)的事实。但这永远行不通,并且值 timeZone永远不会受到约束。

time-zone-picker.html它似乎确实在那里工作。我有<div if.bind="selectedTimeZone">This is working! ->${selectedTimeZone}</div>工作正常。

示例

主模板(create.html):

<template>
<require from="../shared/components/time-zone-picker"></require>
<time-zone-picker selectedTimeZone.two-way="timeZone"></time-zone-picker>
<div if.bind="timeZone">Main: ${timeZone}</div>
</template>

time-zone-picker.html:

<template bindable="selectedTimeZone">
<select class="c-select" value.bind="selectedTimeZone">
<option>Select A Time Zone</option>
<option repeat.for="tz of timezones" model.bind="tz">${tz.text}</option>
</select>
<div if.bind="selectedTimeZone">This is working! ->${selectedTimeZone}</div>
</template>

time-zone-picker.js:

import Timezones from 'timezones.json';

export class TimeZonePicker {
constructor() {
this.timezones = Timezones;
}
}

编辑

添加下面的代码以匹配下面的响应。仍然无法使其与以下更改一起工作:

时区选择器.js

import { bindable, bindingMode } from 'aurelia-framework';
import Timezones from 'timezones.json';

export class TimeZonePicker {
@bindable({ defaultBindingMode: bindingMode.twoWay }) selectedTimeZone;
constructor() {
this.timezones = Timezones;
}
}

时区选择器.html

<template>
<select class="c-select" value.bind="selectedTimeZone">
<option>Select A Time Zone</option>
<option repeat.for="tz of timezones" model.bind="tz">${tz.text}</option>
</select>
<div if.bind="selectedTimeZone">${selectedTimeZone}</div>
</template>

创建.html

<template>
<require from="../shared/components/time-zone-picker"></require>
<time-zone-picker selectedTimezone.two-way="timeZone"></time-zone-picker>
<div if.bind="timeZone">MAIN ${timeZone}</div>
</template>

最佳答案

您应该使用<template bindable="...">仅适用于纯 html 自定义元素。对于您的情况,您应该这样做:

time-zone-picker.html

<template> <-- remove bindable here -->
<select class="c-select" value.bind="selectedTimeZone">
<option>Select A Time Zone</option>
<option repeat.for="tz of timezones" model.bind="tz">${tz.text}</option>
</select>
<div if.bind="selectedTimeZone">This is working! ->${selectedTimeZone}</div>
</template>

time-zone-picker.js:

import {bindable} from 'aurelia-templating'; // or framework
import {bindingMode} from 'aurelia-binding'; // or framework
import Timezones from 'timezones.json';

export class TimeZonePicker {

@bindable({ defaultBindingMode: bindingMode.twoWay }) selectedTimeZone;
constructor() {
this.timezones = Timezones;
}
}

create.html

<template>
<require from="../shared/components/time-zone-picker"></require>
<time-zone-picker selected-time-zone.two-way="timeZone"></time-zone-picker>
<div if.bind="timeZone">Main: ${timeZone}</div>
</template>

关于javascript - Aurelia 双向绑定(bind)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com