javascript - 自定义多选下拉元素中的 checkedChange 事件
问题描述
我有一个multiselect-dropdown.html
带有它自己的.js,以及一个edit-user.html
带有它自己的.js。
当我edit-user.html
单击multiselect-dropdown.html
.
我发现 aurelia 文档有点缺乏信息。
我认为我不需要粘贴 js 文件,我只需要有关如何edit-user.html
从列表项中调用函数的指示,multiselect-dropdown.html
这可能吗?一些提示会有所帮助。
<template>
<div class="dropdown">
<button id="${baseId}"
class="dropdown-toggle form-control input"
type="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
<span class="multiselect-dropdown-selection">${textCaption}</span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu input"
aria-labelledby="${baseId}"
click.trigger="preventDropDownClose($event)">
<li if.bind="!disableOriginal">
<a>
<label>
<input
type="checkbox"
id="multiselect-dropdown-select-default"
checked.bind="isSelectOriginal">
${textSelectOriginal}
</label>
</a>
</li>
<li>
<a>
<label>
<input
type="checkbox"
id="multiselect-dropdown-select-all"
checked.bind="isSelectAll">
${textSelectAll}
</label>
</a>
</li>
<li role="separator" class="divider"></li>
<li repeat.for="option of options" onmousedown=>
<a>
<label>
<input // This should send a trigger to a function in edit-user.html
type="checkbox"
class="multiselect-dropdown-option"
checked.bind="option.checked"> ${option.name}
</label>
</a>
</li>
</ul>
</div>
</template>
这是多选,如果复选框已被选中或未选中,edit-user.html
我如何调用函数?edit-user.html
multiselect-dropdown.html
<div class="form-group user-multiselect-dropdown">
<label for="address" class="regular-15-25">Mina valda adresser</label>
<multiselect-dropdown
options.bind="selectedAddresses"
noun-singular="adress"
noun-plural="adresser">
</multiselect-dropdown>
</div>
解决方案
您需要使用如下属性将函数绑定到您<multiselect-dropdown>
的.call
属性:
<multiselect-dropdown function-name.call="checkboxChanged()"></multiselect-dropdown>
您可以在您的视图模型中绑定到它,multiselect-dropdown
如下所示:
@bindable()
public functionName: () => void; // or without typing if you don't use typescript
注意函数名的大小写;在 HTML 中,您用破折号 ( -
) 分隔的内容在您的视图模型中是驼峰式,例如function-name
变为functionName
现在,只要复选框更改,您只需在视图模型中调用该函数。您可以使用装饰器observe
来获取复选框的值,并在它更改时调用该函数。@observable
如果要观察对象的属性,则需要使用bindingEngine
。
import { bindable, observable, bindingEngine, autoinject } from "aurelia-framework";
@autoinject
export class Foo {
@bindable()
public functionName: () => void;
@observable()
public checkboxValue: boolean;
public option = {
checked: true;
}
constructor(private bindingEngine: BindingEngine) {}
attached() {
// subscribe to an object property
this.subscription = this.bindingEngine.propertyObserver(this.option, "checked").subscribe(() => {
this.functionName();
}
}
detached() {
// Dispose subscription to avoid memory leak
this.subscription.dispose();
}
// Observe a single field for changes
checkboxValueChanged() {
this.functionName();
}
}
推荐阅读
- spring - 无法将类自动连接到我的控制器中的接口,引发异常
- graph - Neo4j 设计性能:我必须避免大节点度吗?
- git - 什么是分布式软件程序?以 GIT 为例
- java - 如何使用带有外键的 PreparedStatement
- ms-access - sql查询中的ODBC错误
- python - 在进程之间共享一个模块
- django - Django 1.8 导入问题
- angular - Angular 5 - 相当于来自 AngularJS 的 $interval
- retrofit2 - onResponse 中的 Retrofit 2.0 条件逻辑不起作用
- aframe - 如何更改框架曲线组件中的曲线类型