按下Enter键时,Primeng将与打字的字符串一起添加空芯片

发布于 2025-02-13 01:44:36 字数 791 浏览 1 评论 0原文

我的代码如下,这只是用于实验目的的一堆代码。 或之间没有空格输入一个

export class AppComponent {
  title = 'imoveis';
   nome:Array<pessoa>  = new Array<pessoa>();
   basic:string = "d";
  
   ngOnInit(){
     let p:pessoa = new pessoa();
     this.nome.push(p);
   }

   adicionar(event:any):void{

    let p:pessoa = new pessoa();
    p.nome = event.value;
    this.nome.push(p);
    
 
 }

}



class pessoa{
   nome:string = "diego"; 
}

我在文本之前,之后

<p-chips (onAdd)="adicionar($event)" [disabled]="false" [max]="2"  [(ngModel)]="nome">
    <ng-template let-item pTemplate="item" >
        {{  item.nome }}
    </ng-template>



  </p-chips>

值src =“ https://i.sstatic.net/0ftse.png” alt =“ erro no Layout Imagem”>

My code is as follows, this is just a bunch of code for experimental purposes. I entered a value with no spaces before, after or between the text but I got "two chips"

export class AppComponent {
  title = 'imoveis';
   nome:Array<pessoa>  = new Array<pessoa>();
   basic:string = "d";
  
   ngOnInit(){
     let p:pessoa = new pessoa();
     this.nome.push(p);
   }

   adicionar(event:any):void{

    let p:pessoa = new pessoa();
    p.nome = event.value;
    this.nome.push(p);
    
 
 }

}



class pessoa{
   nome:string = "diego"; 
}

template

<p-chips (onAdd)="adicionar($event)" [disabled]="false" [max]="2"  [(ngModel)]="nome">
    <ng-template let-item pTemplate="item" >
        {{  item.nome }}
    </ng-template>



  </p-chips>

erro no layout imagem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2025-02-20 01:44:36

因为在使用Diego初始化后,您添加了carlosnome看起来像这样。

[
    {
        "nome": "diego"
    },
    "CARLOS",
    {
        "nome": "CARLOS"
    }
]

模板尝试读取{{item.nome}},但“ Carlos”不是具有属性nome的对象,而是一个字符串。但是Prime-NG芯片仍然可以看到应该有东西,因此它增加了无名的芯片。问题源形式onaddngmodel,他们都将为nome []添加一个值。 ngmodel添加字符串和onadd添加了OBJ Typeof Person。

Primeng芯片旨在容纳各种字符串,而不是物体。

nome: string[] = ['diego', 'CARLOS']

重组:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  nome: string[] = ['diego'];
}
<p-chips [disabled]="false" [max]="2" [(ngModel)]="nome">
  <ng-template let-item pTemplate="item">
    {{ item }}
  </ng-template>
</p-chips>

工作演示: https://stackblitz.com/edit/primeng-chips-demo-hsbneu?file=src%2fapp%2fapp%2fapp.component.ponent.html

Because after you have initialized with diego and you add CARLOS, nome looks like this.

[
    {
        "nome": "diego"
    },
    "CARLOS",
    {
        "nome": "CARLOS"
    }
]

Template tries to read {{ item.nome }} but "CARLOS" is not an object with property nome, it's just a string. But prime-ng chips still sees that something should be there, so it adds a nameless chip. Problem originates form onAdd and ngModel, they both will add a value to nome[]. ngModel adds the string and onAdd adds obj typeof person.

PrimeNG chips is meant to hold array of strings, not objects.

nome: string[] = ['diego', 'CARLOS']

Refactored:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  nome: string[] = ['diego'];
}
<p-chips [disabled]="false" [max]="2" [(ngModel)]="nome">
  <ng-template let-item pTemplate="item">
    {{ item }}
  </ng-template>
</p-chips>

Working demo: https://stackblitz.com/edit/primeng-chips-demo-hsbneu?file=src%2Fapp%2Fapp.component.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文