Aurelia 双向绑定无法正常工作

Aurelia Two-Way Binding not working properly

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

所以我有 create.html,其中有 selectedTimeZone.two-way="timeZone"。我试图通过执行 <div if.bind="timeZone">Main: ${timeZone}</div> 来显示它正在工作并具有约束力的事实。但这永远行不通,并且值 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>

时区-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>

时区-picker.js:

import Timezones from 'timezones.json';

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

编辑

添加下面的代码以匹配下面的响应。仍然无法使其适用于以下更改:

时区-picker.js

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

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

时区-picker.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>

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>

您应该只对 html 自定义元素使用 <template bindable="...">。在你的情况下,你应该这样做:

时区-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>

时区-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>