简体   繁体   English

自定义元素中的Aurelia双向绑定不起作用

[英]Aurelia two-way binding in custom element not working

Trying to learn Aurelia 1.0 and I can't seem to get two-way binding on a custom element to work. 尝试学习Aurelia 1.0,我似乎无法对自定义元素进行双向绑定。

Using bootstrap-datepicker I made a date-range-picker custom element: 使用bootstrap-datepicker,我创建了一个date-range-picker自定义元素:

date-range-picker.js 日期范围picker.js

import {bindable, bindingMode, inject} from 'aurelia-framework';
import $ from 'jquery';
import datepicker from 'bootstrap-datepicker';

@inject(Element)
export class DateRangePicker {
  @bindable startName = 'start';
  @bindable endName = 'end';
  @bindable startValue = null;
  @bindable endValue = null;

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

  attached() {
    $(this.element).find('.input-daterange').datepicker();
  }
}

date-range-picker.html date-range-picker.html

<template>
  <div class="input-daterange input-group" id="datepicker">
    <input type="text" class="input-sm form-control" 
      name="${startName}" id="${startName}" 
      value.two-way="startValue" 
      placeholder="Start Date (mm/dd/yyy)" />
    <span class="input-group-addon">to</span>
    <input type="text" class="input-sm form-control" 
      name="${endName}" id="${endName}" 
      value.two-way="endValue" 
      placeholder="End Date (mm/dd/yyy)"/>
  </div>
</template>

the custom element is used in leads.html: 该定制元素用于Leads.html中:

<div class="col-sm-12">
  <form role="form" class="form-inline" submit.delegate="search()">
    <div class="form-group">
      <date-range-picker 
        start-name="createdAtStartDate" start-value.two-way="startDate" 
        end-name="createdAtEndDate" end-value.two-way="endDate">  
      </date-range-picker>
    </div>
    <button type="submit" class="btn btn-primary">Search</button>
  </form>
</div>

leads.js Leads.js

import {inject} from 'aurelia-framework';
import {LeadsService} from './leads-service';
import moment from 'moment';

@inject(LeadsService)
export class Leads {
  startDate = moment().format('M/D/YYYY');
  endDate = moment().format('M/D/YYYY');
  leads = [];

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

  search() {
    this.dataService.getLeads({
      startDate: this.startDate,
      endDate: this.endDate
    })
   .then(leads => this.leads = leads);
  }
}

date-range-picker works as expected and whatever value is set at startDate and endDate in the leads.js is properly bound to the input boxes in the custom element but when I submit the form startDate and endDate doesn't change even if I change the values of the input boxes. date-range-picker按预期工作,并且Leads.js中startDate和endDate设置的任何值都已正确绑定到自定义元素中的输入框,但是当我提交表单startDate和endDate时,即使我更改了,它也不会更改输入框的值。

Any ideas what I'm doing wrong? 有什么想法我做错了吗?

The values are being updated by javascript, so you have to dispatch the input's change event, like this: 值正在由javascript更新,因此您必须调度输入的change事件,如下所示:

 attached() {
    $(this.element).find('.input-daterange').datepicker()
      .on('changeDate', e => {
        e.target.dispatchEvent(new Event('change'));
      });
  }

Here's a running example https://gist.run/?id=5d047c561cf5973cf98e88bae12f4b4e 这是一个正在运行的示例https://gist.run/?id=5d047c561cf5973cf98e88bae12f4b4e

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM