按下Enter键时,Primeng将与打字的字符串一起添加空芯片
我的代码如下,这只是用于实验目的的一堆代码。 或之间没有空格输入一个
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为在使用
Diego
初始化后,您添加了carlos
,nome
看起来像这样。模板尝试读取
{{item.nome}}
,但“ Carlos”
不是具有属性nome
的对象,而是一个字符串。但是Prime-NG芯片仍然可以看到应该有东西,因此它增加了无名的芯片。问题源形式onadd
和ngmodel
,他们都将为nome []
添加一个值。ngmodel
添加字符串和onadd
添加了OBJ Typeof Person。Primeng芯片旨在容纳各种字符串,而不是物体。
重组:
工作演示: 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 addCARLOS
,nome
looks like this.Template tries to read
{{ item.nome }}
but"CARLOS"
is not an object with propertynome
, it's just a string. But prime-ng chips still sees that something should be there, so it adds a nameless chip. Problem originates formonAdd
andngModel
, they both will add a value tonome[]
.ngModel
adds the string andonAdd
adds obj typeof person.PrimeNG chips is meant to hold array of strings, not objects.
Refactored:
Working demo: https://stackblitz.com/edit/primeng-chips-demo-hsbneu?file=src%2Fapp%2Fapp.component.html