简体   繁体   English

如何在 aurelia 中的多 select 下拉菜单上创建可观察对象

[英]how to create an observable on a multi select drop down in aurelia

i currently have a dropdown that when a user goes and clicks on a single option it automatically gets me the value for filtering as an observable as follows我目前有一个下拉列表,当用户点击一个选项时,它会自动获取我过滤为可观察值的值,如下所示

public months: any=[];
@observable
    public selectedMonth: string= "";
async onLoad() {
        this.months = Moment.months();
        }
public selectedMonthChanged() {
        if (this.selectedMonth != "") {
            this.update();
        }
        
    }
<select  md-select value.bind="selectedMonth">
                        <option value="" disabled>Month</option>
                        <option repeat.for="month of months" value.bind="month" click.delagate="selectedMonthChanged()">${month}</option>
                    </select>

so the above works when i select an option it calls the selectedMonthChanged() function.But now i am trying to add a multiselect as follows and i cant get back a list of the items selected and it doesnt call the selectedMonthChanged() function so the above works when i select an option it calls the selectedMonthChanged() function.But now i am trying to add a multiselect as follows and i cant get back a list of the items selected and it doesnt call the selectedMonthChanged() function

this is what i tried这是我试过的

public months: any=[];
    @observable
        public selectedMonth: any = [];
    async onLoad() {
            this.months = Moment.months();
            }
    public selectedMonthChanged() {
            if (this.selectedMonth != []) {
                this.update();
            }
            
        }
    <select  multiple md-select value.bind="selectedMonth">
                            <option value="" disabled>Month</option>
                            <option repeat.for="month of months" value.bind="month" click.delagate="selectedMonthChanged()">${month}</option>
                        </select>

any idea how i could pass a list of values as an observable?知道如何将值列表作为可观察值传递吗?

A few things here.这里有几件事。 I believe the problem lies with the option having value.bind instead of having model.bind as per the Aurelia 1 docs.我认为问题在于具有value.bind的选项,而不是根据 Aurelia 1 文档具有model.bind的选项。

Secondly I wanted to mention that if you're using Aurelia 2 the @obserable annotation is a little wonky atm even with the latest version.其次,我想提一下,如果您使用的是 Aurelia 2,即使使用最新版本,@obserable 注释也会有点不稳定。 I suggest using the @watch annotation anyway for better code readability.我建议无论如何都使用@watch 注释以获得更好的代码可读性。 I believe this is only available in Aurelia 2 but it might be available in Aurelia 1. Been using Au2 now for awhile.我相信这仅在 Aurelia 2 中可用,但它可能在 Aurelia 1 中可用。现在使用 Au2 有一段时间了。

selectedMonth;

@watch('selectedMonth')
someFunctionName() {
   if (this.selectedMonth != []) {
      this.update();
   }
}

As a note this works for as deep as you'd like into the object.请注意,这可以深入到 object 中。 @watch('someObject.someObject.someObject.someProperty') Pretty neat. @watch('someObject.someObject.someObject.someProperty')整洁。 Can apply multiple @watch annotations to a single function as well.也可以将多个@watch注释应用于单个 function。

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

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