简体   繁体   English

角度,ng-zorro:对象作为标签

[英]angular, ng-zorro: object as tag

I have a nz-select described https://ng.ant.design/components/select/en#api here which allows users to select multiple members. 我在这里有一个nz-select描述, 网址为https://ng.ant.design/components/select/en#api ,它允许用户选择多个成员。 What I want is to show a username as tag label, but have the id bound to the model. 我想要的是显示一个用户名作为标签标签,但是将ID绑定到模型。

<nz-select formControlName="member" nzMode="tags" (nzOnSearch)="searchUsers($event)">
    <nz-option *ngFor="let member of searchUsersList" [nzLabel]="member.name" [nzValue]="member._id">
    </nz-option>
</nz-select>

When the user adds a member via the dropdown, the tag will show the username, but in the model, the id gets added. 当用户通过下拉列表添加成员时,标签将显示用户名,但在模型中会添加ID。 When reloading this page/field, it seems that I only can add strings to the model which are also being shown as label then . 重新加载此页面/字段时,看来我只能将字符串添加到模型中,这些字符串也将显示为label。 I cannot add complex objects which contain the string and id and I also don't know how to insert tags otherwise. 我无法添加包含字符串和ID的复杂对象,而且我也不知道如何插入标签。

Any hints on how to accomplish what I want? 关于如何完成我想要的任何提示? I really don't like the idea of using usernames inside the backend/db but I also don't want to see id`s inside the tags. 我真的不喜欢在后端/数据库中使用用户名的想法,但我也不想在标签中看到id。

I can work. 我能工作。

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl } from '@angular/forms';

@Component({
  selector: 'nz-demo-select-label-in-value',
  template: `
    <nz-select
      [formControl]="control"
      style="width: 200px;"
      nzMode="tags">
      <nz-option *ngFor="let option of optionList"
       [nzValue]="option.value" 
       [nzLabel]="option.label">
      </nz-option>
    </nz-select>
  `
})
export class NzDemoSelectLabelInValueComponent implements OnInit {

  optionList = [{ label: 'Lucy', value: 1,}, { label: 'Jack', value: 2}];

  control: FormControl;

  constructor(private fb: FormBuilder) {

  }

  ngOnInit() {
    this.control = this.fb.control([2]);
  }

}

https://stackblitz.com/edit/angular-73fp31-simfut?file=src/app/app.component.ts https://stackblitz.com/edit/angular-73fp31-simfut?file=src/app/app.component.ts

The problem is that when u reload the page u Destroy all tags. 问题是当您重新加载页面时,销毁所有标签。 when u load the model and the value is pased to the nz-select the options of these ids is not in your searchUsersList atribute. 当您加载模型并将值粘贴到nz-select时,这些id的选项不在您的searchUsersList属性中。 when u reload the page u need get the ids of your form control member and hydrate your searchUsersList with these objects. 当您重新加载页面时,您需要获取表单控件member的ID并将这些对象与您的searchUsersList合并。 Maybe make a http request to the backend with those ids and get the options of those ids 也许使用这些ID向后端发出http请求,并获取这些ID的选项

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

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