【问题标题】:Dropdown not showing values on load下拉菜单未显示加载值
【发布时间】:2019-02-05 17:18:25
【问题描述】:

我的 component.html 中有一个下拉列表,当用户单击编辑按钮时,该下拉列表需要填充值。现在正在检索所有值,包括下拉列表的值,但下拉列表是用户必须单击才能查看值的唯一控件。我希望下拉菜单在用户不点击的情况下显示该值。

这是component.html的代码,

<mat-form-field>
    <mat-select placeholder="Completed" formControlName="completed" #completed>
        <mat-option value="true">Yes</mat-option>
        <mat-option value="false">No</mat-option>
    </mat-select>
</mat-form-field>

这是我的编辑组件类的代码,

ngOnInit() {
    this.route.params.subscribe(params => {

        this.id = parseInt(params.id);
        this.blogService.getBooksById(this.id).subscribe(res => {
            this.book = res;
            this.completed = this.book[0].completed;

            if (this.book[0].completed == true)
                this.completed = "Yes"
            else
                this.completed = "No"

            this.updateForm.get('title').setValue(this.book[0].title);
            this.updateForm.get('comments').setValue(this.book[0].comments);
            this.updateForm.get('completed').setValue(this.completed);
        });
    });
}

所以没有错误。我可以编辑和保存。但我不希望用户单击下拉列表来查看值是什么。下拉列表应显示从表单中检索到的相应书籍的 completed 值。 updateForm 的类型为 FormGroup。谢谢。

【问题讨论】:

  • 您可能的值是字符串“Yes”和字符串“No”,因此在您的选项中,您的值必须是 value="Yes" 和 value="No"
  • 谢谢@Eliseo。我改变了它,然后我继续添加一个函数,将“是”转换为真,将“否”转换为假,因为我在数据库中的字段有一个布尔数据类型

标签: angular angular-material


【解决方案1】:

您需要将this.completed 设置为“true”或“false”,而不是“yes”或“no” 因为这是你的mat-otion的值

代码应如下所示:

ngOnInit() {
    this.route.params.subscribe(params => {

        this.id = parseInt(params.id);
        this.blogService.getBooksById(this.id).subscribe(res => {
            this.book = res;
            this.completed = this.book[0].completed;

            if (this.book[0].completed == true)
                this.completed = "true"
            else
                this.completed = "false"

            this.updateForm.get('title').setValue(this.book[0].title);
            this.updateForm.get('comments').setValue(this.book[0].comments);
            this.updateForm.get('completed').setValue(this.completed);
        });
    });
}

【讨论】:

    【解决方案2】:

    根据我对我们问题的理解,只需将模板文件更新为:

    <mat-form-field>
        <mat-select placeholder="Completed" [(ngModel)]="completed" #completed>
            <mat-option value="true">Yes</mat-option>
            <mat-option value="false">No</mat-option>
        </mat-select>
    </mat-form-field>
    

    并从您的 ts 文件中删除 this.updateForm.get('completed').setValue(this.completed); 行。

    由于我没有任何外部 API 数据的来源,我创建了一个演示 Here

    如果这对您有帮助,请告诉我。

    【讨论】:

    • 谢谢桑克特。所以我试了一下,但我收到一条错误消息,指出 Can't bind to 'ngModel' since it isn't a known property of 'mat-select'.
    • 您是否已将FormsModule 导入到您的module.ts 文件中?
    • 我导入了 FormsModule,然后又遇到了另一个错误 ngModel cannot be used to register form controls with a parent formGroup directive. Try using formGroup's partner directive "formControlName" instead. 看起来它需要对当前代码进行一些修改。我现在将坚持当前的解决方案,也许稍后再谈。再次感谢 Sanket。
    • 您能否通过 stackBlitz 向我发送指向我们代码的链接?
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 2014-06-01
    • 2019-01-10
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多