【问题标题】:Selection in Angular Dart material dropdown select gives errorAngular Dart 材料下拉选择中的选择给出错误
【发布时间】:2019-03-16 16:49:51
【问题描述】:

我有一个带有材质下拉选择的组件,当 [(selection)] 的类型为 SingleSelectionModel 时,我收到错误消息“角镖预期的值类型为‘SingleSelectionModel’,但得到的类型为‘Term’”。当它是 Term 类型时,它很好。 我需要 SingleSelectionModel 来访问其更改流并对选定的值进行一些操作。

我有 Dart 2.2.0,角度:^5.2.0,角度组件:^0.11.0。

谁能帮助如何将 SingleSelectionModel 用于 [(selection)]?

@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';
}

【问题讨论】:

    标签: dart angular-dart


    【解决方案1】:

    终于找到了解决办法

    我已经将模板绑定 [()] 拆分为 []() 所以模板是

      template: '''
        <material-dropdown-select
            [options]="terms"
            [selection]="selectedTerm"
            (selectionChange)="validateTerm($event)"
            [buttonText]="buttonText">
        </material-dropdown-select>
      ''',
    

    在 Dart 文件中,我有一个方法 validateTerm(Term term),它会进行一些验证检查并将选定的术语分配给 selectedTerm。所以我不需要监视选择更改流。

     Term selectedTerm;
    
     void validateTerm(Term term) {
        // do some validations...
        selectedTerm = term;
      }
    

    【讨论】:

    • 嗨,你能在 github 上分享你的代码吗?我有一个错误:EXCEPTION: Assertion failed: org-dartlang-app:///packages/angular_components/material_select/material_dropdown_select_accessor.dart:118:12 _select.selection == null "Cannot set [selection] when using a Dropdown control value accessor."
    • 无法共享代码,因为它是非常大的项目的一部分。正如我刚刚检查过的,我使用了上一个答案(更新)中描述的解决方案。
    • 我不确定这是错误还是正常行为。我使用[selection] 显示值并使用(selectionChange) 设置值。这样就可以正常工作了。
    • 好的,谢谢,我成功实现了活动。
    猜你喜欢
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多