简体   繁体   English

Angular7-自动填充ng-select

[英]Angular7 - Autofill ng-select

I have a view with an id in URL. 我有一个ID为URL的视图。 When loading this view I call service and take all user data, looks similar to. 加载此视图时,我致电service并获取所有用户数据,看起来类似于。

{
    "code": 0,
    "message": "",
    "body": {
        "id": 1,
        "name": "test",
        "profile": [
            {
                "id": 2,
                "description": "admin"
            },
            {
                "id": 1,
                "description": "user"
            }
        ]
    }
}

Suppose that user doesn't have profiles, so select component only has all array data. 假设用户没有配置文件,那么select组件仅具有所有数组数据。 In other case suppose that user has an admin profile, so select component has all array but admin is the default selected. 在其他情况下,假设用户具有管理员配置文件,则select组件具有所有阵列,但是admin是默认选择的组件。

In HTML i have 在HTML我有

 <ng-select [multiple]="true"  name="perfiles" placeholder="{{ 'profile-placeholder' | translate }}"
        [(ngModel)]="perfiles">
        <ng-option *ngFor="let perfil of perfiles"  bindLabel="perfil.descripcion" [value]="perfil.id" >{{perfil.descripcion}}</ng-option>
      </ng-select>

And the result is. 结果是。

NG-选择

The problem is that component doesn't show any data. 问题在于该组件不显示任何数据。 Apparently works, a user has two profiles, but I don´t know how show description. 显然可以使用,一个用户有两个配置文件,但是我不知道如何显示描述。 Any help? 有什么帮助吗?

I think you made a typo in your html: 我认为您在html中打了一个错字:

    <ng-option *ngFor="let perfil of perfiles"  bindLabel="perfil.descripcion" [value]="perfil.id" >{{perfil.descripcion}}</ng-option>

it should be: 它应该是:

    <ng-option *ngFor="let perfil of perfiles"  bindLabel="perfil.description" [value]="perfil.id" >{{perfil.description}}</ng-option>

I changed {{perfil.descripcion}} to {{perfil.description}} and bindLabel="perfil.descripcion" to bindLabel="perfil.description" according to your JSON, this should work now. 我已根据您的JSON将{{perfil.descripcion}}更改为{{perfil.description}}并将bindLabel="perfil.descripcion"更改为bindLabel="perfil.description" ,现在应该可以使用了。

I found the solution. 我找到了解决方案。 In first time I need to get all elements in a array with a web service. 第一次,我需要使用Web服务获取数组中的所有元素。 Then we need to save all elements in a variable. 然后,我们需要将所有元素保存在变量中。

//Declare a variable 
private allElements: any= [];
//Save web response data in a variable
this.allElements= response["body"];

We need to do same to get user elements in array. 我们需要做同样的事情才能将用户元素放入数组。 We need new web service for search user elements. 我们需要用于搜索用户元素的新Web服务。

//Declare a variable 
private userElements: any= [];
//Save web response data in a variable. This response only has user data.
this.userElements= response["body"];

Finally in HTML we need. 最后,我们需要HTML。

 <ng-select [items]="allElements"  [multiple]="true" name="userElements" bindLabel="description"
 [(ngModel)]="userElements">
 </ng-select>

This create a loop over allElements to fill select component and show userElements as default. 这将在allElements上创建一个循环以填充选择组件并显示userElements作为默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM