Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 材料选择值未显示_Angular_Typescript_Angular Material - Fatal编程技术网

Angular 材料选择值未显示

Angular 材料选择值未显示,angular,typescript,angular-material,Angular,Typescript,Angular Material,我的视图中有一个角度组件amat select。 此角度组件具有用于编辑某些数据的表单。因此,当我打开组件视图时,表单是预先填充的 <mat-form-field> <mat-select [(value)]="selectedGroups" placeholder="Select the associated Bitbucket groups ..." multiple>

我的
视图中有一个
角度组件
a
mat select
。 此
角度组件
具有用于编辑某些数据的表单。因此,当我打开组件视图时,表单是预先填充的

  <mat-form-field> 
          <mat-select
            [(value)]="selectedGroups"
            placeholder="Select the associated Bitbucket groups ..."
            multiple>

            <mat-option
            *ngFor="let group of bitbucketgroupsList?.results"
            [value]="group.id">{{group.bitbucket_group}}
            </mat-option>
           </mat-select> 
   </mat-form-field>
ngOnInit
中,我要做的是发送一个
HTTP请求
来获取团队的实际数据,并在表单中显示它们(在表单前填充),然后让用户更改它们。 我的问题是,当我打开具有表单的视图时,selectedGroups的值没有在mat select字段中被选中。即使我有另一个具有相同逻辑的组件,它也可以正常工作。在这里,我不知道我错过了什么。 当我执行
console.log(this.selectedGroups)
时,我看到列表有一个值,但它没有显示在
mat select
中。
有什么线索吗?

使用compareFn函数告诉Angular如何比较值

要自定义默认选项比较算法, 支持与输入进行比较。compareWith接受一个具有两个 参数:选项1和选项2。如果给定了compareWith,则为 通过函数的返回值选择选项

参考:


{{topping.name}

示例:

使用compareFn函数,告诉Angular如何比较值

要自定义默认选项比较算法, 支持与输入进行比较。compareWith接受一个具有两个 参数:选项1和选项2。如果给定了compareWith,则为 通过函数的返回值选择选项

参考:


{{topping.name}

示例:

try[(ngModel)]=“selectedGroups”ngModel在Angular 6中不受欢迎,我使用的是被动形式,因此ngModel不兼容。它给出了以下错误:
error error:“ngModel不能用于向父formGroup指令注册表单控件
您可以发布
控制台.log()的输出吗
?那么表单控件在code@Chellappan对于简单输入,我使用formControl。对于选择输入,我使用[(值)]尝试[(ngModel)]=”selectedGroups“ngModel在Angular 6中被弃用,我使用的是被动表单,因此ngModel不兼容。它会出现以下错误:
错误错误:“ngModel不能用于使用父formGroup指令注册表单控件
您能发布
控制台的输出吗。log()
?那么表单控件在code@Chellappan我使用formControl进行简单的输入。对于选择输入,我使用[(值)]
selectedGroups = [];
ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
        this.id = +params['id'];
    });
    this.EditteamForm = this._fb.group({
        name: ['', Validators.required],
        description: ''
    });
    let url = this.baseUrl + `/teams/${this.id}`;
    this.httpClient.get(url).subscribe((data: Array<any>) => {
        this.EditteamForm.controls['name'].setValue(data['results'][0]['name']);
        this.EditteamForm.controls['description'].setValue(data['results'][0]['description']);
        for (let i = 0; i < data['results'][0]['bitbucket_group'].length; i++) {
            let value = data['results'][0]['bitbucket_group'][i]['id'];
            this.selectedGroups.push(value);
        }

    });
}
<mat-form-field>
  <mat-select placeholder="Toppings"
  [compareWith]="compareFn" 
   [(value)]="toppings" multiple>
    <mat-option *ngFor="let topping of toppingList" [value]="topping">{{topping.name}}</mat-option>
  </mat-select>
</mat-form-field>