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

Ema*_*oha 2 attributes angular

我正在使用 ng2-select 单击此处!lib 但这个错误是显示:

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

  • 使用 angular2
  • 在 html 文件中与顶部的链接相同
  • 在 ts 我的组件与顶部的链接相同
  • 在 modules.ts 添加: import { SelectModule } from '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>
Run Code Online (Sandbox Code Playgroud)

在 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;
}
}
Run Code Online (Sandbox Code Playgroud)

感谢您。

Ani*_*had 8

您应该像这样将ngDefaultControl属性添加到您的输入中:

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

  • 请解释它的作用(使用链接或您自己的描述) (2认同)