我正在Aurelia中构建一个多步骤表单,每个页面显示一个问题。只有null或数组实例可以绑定到多选
我对每个问题都使用相同的视图,并使用if
语句确定要显示的表单类型字段。
当我尝试将我的问题数据绑定到多选元素时,Aurelia会抛出错误并说“只有null或Array实例可以绑定到多选。”。
真奇怪的是,如果第一个问题是一个多重选择,我不会收到错误,直到我来到一个非多选问题,然后回到多选问题。
我可以通过为此路线设置activationStrategy: 'replace'
来解决整个问题,但我真的不希望这样。
重要的代码如下:
import {inject} from 'aurelia-framework';
import {Router} from 'aurelia-router';
@inject(Router)
export class Form {
constructor (router) {
this.router = router;
this.active = 0;
this.field = null;
this.fields = [
{
type: 'text',
value: null
},
{
type: 'select',
value: [],
options: [
'foo',
'bar'
]
},
{
type: 'select',
value: [],
options: [
'foo',
'bar'
]
},
{
type: 'text',
value: null
},
];
}
activate (routeParams) {
this.active = routeParams.fieldIndex || 0;
this.active = parseInt(this.active);
this.field = this.fields[this.active];
}
prev() {
if (typeof this.fields[this.active - 1] !== 'undefined') {
this.router.navigateToRoute('form', {
fieldIndex: this.active - 1
});
return true;
}
else {
return false;
}
}
next() {
if (typeof this.fields[this.active + 1] !== 'undefined') {
this.router.navigateToRoute('form', {
fieldIndex: this.active + 1
});
return true;
}
else {
return false;
}
}
}
而且模板:
<template>
<div class="select" if.bind="field.type == 'select'">
<select value.bind="field.value" multiple="multiple">
<option repeat.for="option of field.options" value.bind="option">${option}</option>
</select>
</div>
<div class="text" if.bind="field.type == 'text'">
<input type="text" value.bind="field.value">
</div>
<a click.delegate="prev()">Previous</a> | <a click.delegate="next()">Next</a>
</template>
但你可能会想检查出GistRun:https://gist.run/?id=4d7a0842929dc4086153e29e03afbb7a,以获得更好的理解。
尝试将第一个问题设置为多选,并且您会注意到错误消失(直到您回到它)。您也可以在app.js中尝试activationStrategy
,如上所述。
为什么会发生这种情况,我该如何解决?
另请注意,在我的真实应用程序中,我实际上使用compose
而不是if
s,但已尝试使用这两种方法,两者都产生相同的错误。看起来好像选择值在if被评估之前被绑定,导致错误出现,因为text
字段类型缺少options
数组。
我认为这个问题归结于在'
@thebluefox感谢您的帮助。但是,我不遵循:/从选项中删除'value.bind'没有帮助。请注意,在某些情况下,代码工作正常,所以我怀疑实际的数据绑定有什么问题。 – powerbuoy
通过这种逻辑,虽然没有任何代码是错误的,因为它在所有情况下都能正常工作;)但我明白了关于该答案的观点。我刚刚测试了绑定'