Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Aurelia并选择2更改事件_Javascript_Jquery Select2_Select2_Aurelia - Fatal编程技术网

Javascript Aurelia并选择2更改事件

Javascript Aurelia并选择2更改事件,javascript,jquery-select2,select2,aurelia,Javascript,Jquery Select2,Select2,Aurelia,如何利用Aurelia与Select2的数据绑定?它似乎与一个标准的选择工作得很好 理想情况下,我希望在select元素中使用change.delegate调用视图模型中的方法,这样我就可以访问我们正在注入的数据存储 触发事件的唯一方法是在attached()处理程序中连接一个更改事件,但随后数据存储超出范围 视图: <template> <label>Company:</label> <!-- i would like to use c

如何利用Aurelia与Select2的数据绑定?它似乎与一个标准的选择工作得很好

理想情况下,我希望在select元素中使用change.delegate调用视图模型中的方法,这样我就可以访问我们正在注入的数据存储

触发事件的唯一方法是在attached()处理程序中连接一个更改事件,但随后数据存储超出范围

视图:

<template>
    <label>Company:</label>
    <!-- i would like to use change.delegate="change()" below -->
    <select value.bind="selectedCompanies" multiple>  
        <option repeat.for="company of companies" model.bind="company.CompanyId">${company.CompanyName}</option>
    </select>
</template>

另一方面,奥雷莉亚没有为新活动内置聚填充胶吗?我似乎不喜欢那样

我看到这个问题越来越老了,但我也试着像你在这里所做的那样,最后得出以下结论:

我不使用polyfill,也不使用组件外部的原始事件。我对选项和所选值使用双向绑定。也许这对你也有用


它是用TypeScript编写的,但它几乎与JavaScript相同,因此我认为您可以对其进行修改:)

我看到这个问题越来越老了,但我尝试了与您在这里所做的相同的操作,并得出以下结论:

我不使用polyfill,也不使用组件外部的原始事件。我对选项和所选值使用双向绑定。也许这对你也有用

它是用TypeScript编写的,但它几乎与JavaScript相同,所以我认为您可以修改它:)

如果您使用
newevent('change',{bubbles:true})change.delegate
应该工作更改
。代理
应该可以工作
import {inject, bindable} from 'aurelia-framework'; 
import {FilterCompanyData} from 'data/custom elements/filters/filterCompanyData';
import {UserStorage} from 'storage/userStorage';

@inject(Element, FilterCompanyData, UserStorage)
export class FilterCompanyCustomElement {
    @bindable selectedCompanies;

    constructor(element, filterCompanyData, userStorage) {
        this.element = element;
        this.filterCompanyData = filterCompanyData;
        this.userStorage = userStorage;
        this.companies = [];
    }

    bind(bindingContext, overrideContext) {
        let userId = this.userStorage.userId;

        return this.filterCompanyData
            .getCompanies(userId)
            .then(response => this.companies = response);
    }

    attached() {
        var el = $(this.element).find('select');
        var sel = el.select2({ 
            closeOnSelect: false
        });

        // Is it possible to get rid of this? 
        sel.on('change', function (event) {
            if (event.originalEvent) { return; }

            // TODO add changed data to user storage

            var IE = ((navigator.userAgent.indexOf("MSIE") != -1) || (!!document.documentMode == true));
            var notice = IE ? new CustomEvent('change', { bubble: false }) : new Event('change', { bubble: false });

            $(el)[0].dispatchEvent(notice);
        });
    }

    detached() {
        $(this.element).find('select').select2('destroy');
    }

    change() {
        // I'd like to use this method from change.delegate
        // TODO add changed data to user storage
    }
}