gpt4 book ai didi

angular - Angular 2 没有用于具有未指定名称属性的表单控件的值访问器

转载 作者:行者123 更新时间:2023-12-02 14:11:40 24 4
gpt4 key购买 nike

我正在使用 ng2-select Click here !lib 但显示此错误:

未指定名称属性的表单控件没有值访问器

  • 使用 Angular2
  • 在 html 文件中,与顶部的链接相同
  • 在 ts 中我的组件与顶部的链接相同
  • 在modules.ts中添加:从 'ng2-select' 导入 { SelectModule } ;和 :进口:[选择模块,]

谁能帮我解决这个错误吗?

 <div style="width: 300px; margin-bottom: 20px;">
<h3>Select a single city</h3>
<ng-select [allowClear]="true"
[items]="items"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="No city selected">
</ng-select>
<p></p>
<pre>{{value.text}}</pre>
<div>
<button type="button" class="btn btn-primary"
[(ngModel)]="disabledV" btnCheckbox
btnCheckboxTrue="1" btnCheckboxFalse="0">
{{disabled === '1' ? 'Enable' : 'Disable'}}
</button>
</div>
</div>

在 ts 组件中:

 import { Component, OnInit } from '@angular/core';
import { SelectItem } from 'ng-select/ng2-select'
@Component({
selector: 'app-api-two',
templateUrl: './api-two.component.html',
styleUrls: ['./api-two.component.css'],
})
export class ApiTwoComponent implements OnInit {

constructor() { }

ngOnInit() {
}
public items:Array<string> = ['Amsterdam', 'Antwerp', 'Athens',
'Barcelona',
'Berlin', 'Birmingham', 'Bradford', 'Bremen', 'Brussels', 'Bucharest',
'Budapest', 'Cologne', 'Copenhagen', 'Dortmund', 'Dresden', 'Dublin',
'Düsseldorf', 'Essen', 'Frankfurt', 'Genoa', 'Glasgow', 'Gothenburg',
'Hamburg', 'Hannover', 'Helsinki', 'Kraków', 'Leeds', 'Leipzig', 'Lisbon',
'London', 'Madrid', 'Manchester', 'Marseille', 'Milan', 'Munich',
'Málaga',
'Naples', 'Palermo', 'Paris', 'Poznań', 'Prague', 'Riga', 'Rome',
'Rotterdam', 'Seville', 'Sheffield', 'Sofia', 'Stockholm', 'Stuttgart',
'The Hague', 'Turin', 'Valencia', 'Vienna', 'Vilnius', 'Warsaw',
'Wrocław',
'Zagreb', 'Zaragoza', 'Łódź'];

private value:any = {};
public valueName:string='';
private _disabledV:string = '0';
private disabled:boolean = false;

private get disabledV():string {
return this._disabledV;
}

private set disabledV(value:string) {
this._disabledV = value;
this.disabled = this._disabledV === '1';
}

public selected(value:any):void {
console.log('Selected value is: ', value);
}

public removed(value:any):void {
console.log('Removed value is: ', value);
}

public typed(value:any):void {
console.log('New search input: ', value);
}

public refreshValue(value:any):void {
this.value = value;
}
}

谢谢你。

最佳答案

您应该将 ngDefaultControl 属性添加到您的输入中,如下所示:

<div>
<button type="button" class="btn btn-primary"
[(ngModel)]="disabledV" btnCheckbox
btnCheckboxTrue="1" btnCheckboxFalse="0" ngDefaultControl>
{{disabled === '1' ? 'Enable' : 'Disable'}}
</button>
</div>

关于angular - Angular 2 没有用于具有未指定名称属性的表单控件的值访问器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52159955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com