ng2-select
ng2-select copied to clipboard
How do I get started with the multi select for an angular cli project ?
These are the steps I followed
- npm i ng2-select
- in app.module.ts, I imported SelectModule
- in my component controller, added the code import { Component } from '@angular/core';
@Component({
selector: 'multiple-demo',
templateUrl: './multiple-demo.html'
})
export class MultipleDemoComponent {
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', 'Leeds', 'Leipzig', 'Lisbon', 'Łódź', 'London', 'Kraków', 'Madrid',
'Málaga', 'Manchester', 'Marseille', 'Milan', 'Munich', 'Naples', 'Palermo',
'Paris', 'Poznań', 'Prague', 'Riga', 'Rome', 'Rotterdam', 'Seville', 'Sheffield',
'Sofia', 'Stockholm', 'Stuttgart', 'The Hague', 'Turin', 'Valencia', 'Vienna',
'Vilnius', 'Warsaw', 'Wrocław', 'Zagreb', 'Zaragoza'];
private value:any = ['Athens'];
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 refreshValue(value:any):void {
this.value = value;
}
public itemsToString(value:Array<any> = []):string {
return value
.map((item:any) => {
return item.text;
}).join(',');
}
}
- In my component added
<div style="width: 300px; margin-bottom: 20px;">
<h3>Select multiple cities</h3>
<ng-select [multiple]="true"
[items]="items"
[disabled]="disabled"
(data)="refreshValue($event)"
(selected)="selected($event)"
(removed)="removed($event)"
placeholder="No city selected"></ng-select>
<pre>{{itemsToString(value)}}</pre>
<div>
<button type="button" class="btn btn-primary"
[(ngModel)]="disabledV" btnCheckbox
btnCheckboxTrue="1" btnCheckboxFalse="0">
{{disabled === '1' ? 'Enable' : 'Disable'}}
</button>
</div>
</div>
- When I run the app, I get this error
EXCEPTION: Uncaught (in promise): Error: Error in ./SearchComponent class SearchComponent - inline template:12:12 caused by: No value accessor for form control with unspecified name attribute
Error: Error in ./SearchComponent class SearchComponent - inline template:12:12 caused by: No value accessor for form control with unspecified name attribute
at ViewWrappedError.ZoneAwareError (http://localhost:4200/vendor.bundle.js:140040:33)
How you fix this?
I have the same issue
Same issue here....