我有一个带有材料下拉选择的组件,当[(选择)]类型为SingleSelectionModel时,我得到一个错误“angular dart预期类型为'SingleSelectionModel'的值,但得到'Term'类型之一”。当它的类型为Term时,它很好。我需要SingleSelectionModel来访问其更改流并使用所选值执行某些操作。
我有Dart 2.2.0,angular:^ 5.2.0,angular_components:^ 0.11.0。
任何人都可以帮助如何使用SingleSelectionModel进行[(选择)]吗?
@Component(
selector: 'x-plan-edit-rule',
template: '''
<material-dropdown-select
[options]="terms"
[(selection)]="selectedTerm"
[buttonText]="buttonText">
</material-dropdown-select>
''',
directives: const [
coreDirectives,
MaterialButtonComponent,
MaterialDropdownSelectComponent,
],
)
class EditPlanRuleComponent implements OnInit {
@Input() Rule rule;
SelectionOptions<Term> terms;
SingleSelectionModel<Term> selectedTerm = SelectionModel<Term>.single(); // GIVES ERROR
//Term selectedTerm; // WORKS FINE WITHOUT ERROR
EditPlanRuleComponent();
@override
ngOnInit() {
// List<Term> rule.availableTerms
terms = SelectionOptions.fromList(rule.availableTerms);
selectedTerm?.selectionChanges?.listen((_) {
// do some checks after the selection change
});
}
String get buttonText => 'Some button text';
}
最后我找到了一个解决方案,大多是偶然的。使用时
SingleSelectionModel<Term> selectedTerm = SelectionModel<Term>.single();
模板绑定必须只有[]而不是[()],所以正确的模板是
template: '''
<material-dropdown-select
[options]="terms"
[selection]="selectedTerm"
[buttonText]="buttonText">
</material-dropdown-select>
''',