Error on a dropdown select [duplicate] - html

This question already has an answer here:
ngRepeat:dupes - duplicates in repeater with nested ngrepeat and empty strings
(1 answer)
Closed 5 years ago.
I found this error on a dropdown menu like this:
<div class='dropdown'>
<span>Seleziona Nazione: </span>
<select class='opzioni' ng-model="nazioni">
<option ng-repeat="nazione in nazioni" value='nazione'>{{nazione}}
</option>
</select>
</div>
In my controller i have this code:
$scope.nazioni = ['Austria', 'Belgio', 'Bulgaria', 'Croazia', 'Danimarca', 'Finlandia', 'Francia', 'Germania', 'Grecia', 'Inghilterra', 'Italia', 'Norvegia', 'Olanda', 'Polonia', 'Portogallo', 'Rep. Ceca', 'Romania', 'Russia', 'Spagna', 'Turchia', 'Svezia', 'Svizzera', 'Ucraina'];
When i run it and i select a Nation, i can read this error in the console:
angular.js:14525 Error: [ngRepeat:dupes]
http://errors.angularjs.org/1.6.4/ngRepeat/dupes?p0=nazione%20in%20nazioni&p1=string%3An&p2=n
I got the same error again when I use a list that come from database, like this one :
$http.get('/api/teams').then(function(response){
$scope.teams = response.data;
console.log(response.data);
});
And in the html :
<div class='SquadraCasa'>
<span>Seleziona Squadra Casa: </span>
<select class='opzioni' ng-model="teams">
<option ng-repeat="team in teams" value='team.Nome'>{{team.Nome}}
</option>
</select>
</div>

Add the track by $index
<div class='dropdown'>
<span>Seleziona Nazione: </span>
<select class='opzioni' ng-model="nazioniSelected">
<option ng-repeat="nazione in nazioni track by $index" value='{{nazione}}'>{{nazione}}
</option>
</select>
</div>
also can improve like this,
<div class='dropdown'>
<span>Seleziona Nazione: </span>
<select ng-options="item as item for item in nazioni track by $index" class='opzioni' ng-model="nazioniSelected">
<option value="">Select...</option>
</select>
</div>
This is because your array isnt identificable. Also you ng-model need to be diferent to the array you iterate.

Aside from your error, it is worth pointing out that the 'value' of all your dropdown items will be the same, as you only update the displayed text dynamically based on which item you're currently iterating over
So I'd try this:
<option ng-repeat="nazione in nazioni" value='{{nazione}}'>{{nazione}}
</option>

Remove the value attribute from the option tag:
<div class='dropdown'>
<span>Seleziona Nazione: </span>
<select class='opzioni' ng-model="nazioni">
<option ng-repeat="nazione in nazioni">{{nazione}}
</option>
</select>
</div>
or change it to bind to the data properly:
<div class='SquadraCasa'>
<span>Seleziona Squadra Casa: </span>
<select class='opzioni' ng-model="teams">
<option ng-repeat="team in teams" value='{{team.Nome}}'>{{team.Nome}}
</option>
</select>
</div>
Having a simple string in the value attribute ('nazioni') will result in all your options having the same value ('nazioni') when you really want the values to be each of the nations in the list you provided.

You have duplicate keys, use track by $index a.e.:
<select class='opzioni' ng-model="selectedNazioni">
<option ng-repeat="nazione in nazioni track by $index" >{{nazione}}
</option>
</select>
Controller:
$scope.nazioni = ['Austria', 'Belgio', 'Bulgaria', 'Croazia', 'Danimarca', 'Finlandia', 'Francia', 'Germania', 'Grecia', 'Inghilterra', 'Italia', 'Norvegia', 'Olanda', 'Polonia', 'Portogallo', 'Rep. Ceca', 'Romania', 'Russia', 'Spagna', 'Turchia', 'Svezia', 'Svizzera', 'Ucraina'];
// set default value
$scope.selectedNazioni = 'Austria';
DEMO

Related

How can I set the value for an HTML <select>?

I needed to set a value for < select>
so I added a "value=.." to the element thinking that it will give it a default value.
Is there any way other than selected to use, because in my case the value changes from a candidate to an other.
I did this so the user can see first the candidate's service then change it in case of an error..
<span >Service</span>
<select class="form-control item pb-2" name="service" value={{ $candidate->service->service_name }}>
<option value="Outsourcing"> <span class="font-weight-bold">Outsourcing</span></option>
<option value="Developpement"> <span class="font-weight-bold">Developpement</span></option>
<option value="Call center"> <span class="font-weight-bold">Call center</span></option>
</select>
You cannot set a default value for select. Instead, you can do something like this:
<select class="form-control item pb-2" name="service" >
<option value={{ $candidate->service->service_name }}>{{ $candidate->service->service_name }}</option>
<option value="Outsourcing"> <span class="font-weight-bold">Outsourcing</span></option>
<option value="Developpement"> <span class="font-weight-bold">Developpement</span></option>
<option value="Call center"> <span class="font-weight-bold">Call center</span></option>
</select>
Any option can be set to selected and that will be the default value for the select. That is the standard way of setting the value. Why not use it?
Also options do not have tags inside
<select class="form-control item pb-2" name="service" >
<option selected value="{{ $candidate->service->service_name }}">{{ $candidate->service->service_name }}</option>
<option value="Outsourcing">Outsourcing</option>
<option value="Development">Development</option>
<option value="Call center">Call center</option>
</select>
If you must insert a new option and select it then you can use JavaScript
window.addEventListener("load",function() {
const sel = document.querySelector("[name=service]");
const val = "{{ $candidate->service->service_name }}";
const opts = [new Option(val,val),...sel.options]
sel.length = 0;
opts.forEach(opt => sel.appendChild(opt));
sel.value = val;
})
<span>Service</span>
<select class="form-control item pb-2" name="service">
<option value="Outsourcing">Outsourcing</span></option>
<option value="Development">Development</option>
<option value="Call center">Call center</option>
</select>

Bootstrap Badge Inside Select Option

I have following drop-down :
<select class="form-control">
<option value=1>Pooja Parikh</option>
<option value=2>Poorti Parikh</option>
<option value=3>Agent 2</option>
<option value=6>AWS Bot AWS Bot</option>
</select>
I want the above dropdown with a bootstrap badge, like following
<select class="form-control">
<option value=1>Pooja Parikh</option>
<option value=2>Poorti Parikh</option>
<option value=3>Agent 2</option>
<option value=6>AWS Bot AWS Bot <span class='badge badge-info'>Bot</span> </option>
</select>
when I tried the above code, its giving simple dropdown options with text only (not giving badge)
I was wondering if anyone one helps me out to implement the above.
Thanks

Angular6 how to keep adding dropdown with selected values

Beginner here...user can select filter values from drop down .Each selection should add a new drop down with selected value..intention is user can edit the selection the way they want and select as many filter they want...how to implement this in angular 6?any pointers..thanks for help..
Please check this, I done this from an assumption. I think you can reach your need by using this code.
html
<div>
<div id="AddItem">
<h2>Add State/City</h2>
<select [(ngModel)]="dataDetails" (change)="addData("")">
<option value="">-- choose an item --</option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
</div>
<div *ngFor="let item of items;">
<select [(ngModel)]="selectedData" (change)="addData(item.name)">
<option value="item.name">{{item.name}}</option>
</select>
</div>
<div>
{{a}}
a: <input [(ngModel)]="a" ng-change="changeData()" />
b: <input [(ngModel)]="b" />
</div>
</div>
js
items:any = [];
addData(data=""){
if(this.dataDetails || data){
if(!data)
data = this.dataDetails;
this.items.push({
name: data
});
}
}

Repeat code on button click with AngularJS

I want to create a page where you can add from 1 to n products dinamically.
I've been working with AngularJS for a couple of months but it's kind of difficult for me to think this.
The html code I tought about it's something like this:
https://plnkr.co/edit/2MYbkYeAf4lZSRIh2c8l?p=preview
Here it is the entire template:
<div class="container text-center">
<h1>Make a sale</h1>
<br>
<form>
<div class = "divedp">
<label for="idNumeroFactura" class="col-form-label" >Invoice number: </label>
<input name="inpNumeroFactura" id="idNumeroFactura" maxlength= "8" type="text" ng-model="numeroFactura" class= "form-control"/>
</div>
<h6 class="small">products</h6>
<div class = "divedp">
<label for="idProducto">Product: </label>
<select name="sltProducto" ng-options ="producto.nombreProducto for producto in productos"
ng-model="producto" class="form-control input-sm" id="idProducto">
<option value="">-Choose the product-</option>
</select>
<br>
<label for= "idCantidad" >Quantity: </label>
<select ng-model="cantidad" id="idCantidad" class="form-control input-sm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<button ng-click="showDiv=true; agregarProducto()" class="btn btn-default" ng-hide="showDiv"> Add another product</button>
</div>
<div ng-show="showDiv"> Here should appear the new product</div>
<button ng-click="guardarVenta()" class="btn btn-primary" type="submit">Save sale</button>
</form>
<span >{{mensaje}}</span> <br>
</div>
And this is the part I want to repeat:
<div class = "divedp">
<label for="idProducto">Product: </label>
<select name="sltProducto" ng-options ="producto.nombreProducto for producto in productos"
ng-model="producto" class="form-control input-sm" id="idProducto">
<option value="">-Choose the product-</option>
</select>
<br>
<label for= "idCantidad" >Quantity: </label>
<select ng-model="cantidad" id="idCantidad" class="form-control input-sm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<button ng-click="showDiv=true; agregarProducto()" class="btn btn-default" ng-hide="showDiv"> Add another product</button>
</div>
So when I click add another product button, all div code part should repeat below the first div. This way, the user can choose all products he want with the quantity he want.
I was thinking on creating a template, but this is already a template... I'm using ui-router to do the routering part, but I never repeated a template inside a template, and I never used a template inside a template at all.
Besides, this approach gives me some doubts:
How can I concatenate an attribute on a Json array? (I use Json to communicate between frontend and backend)
How can I avoid users to repeat the same product item? should I take this problem to an AngularJS controller or can I handle it with directives?
Thanks for reading.
Since you didn't show any controller, I can provide only abstract idea of solution. Here goes anyway.
Repeating
Simple looping over collection of data is done with ngRepeat. In your controller, you need to manipulate the collection and angular will propagate the change for you
<div ng-repeat="productBlock in productBlocks track by productBlock.id">
<!-- your repeated code here -->
<button ng-click="removeProductBlock(productBlock.id)"> remove </button>
</div>
<button ng-click="addProductBlock()"> add </button>
ngRepeat documentation
Prohibiting user from selecting duplicate product
You can achieve this, again, with manipulating a collection in your controller. This time, the collection would hold products still available to choose. Angular will help you with propagating the change from controller to your view with the ngOptions directive
<select
ng-options="product as product.label for product in productsAvailable track by product.id"
ng-change="selectProduct(product.id)">
</select>
The selectProduct method will be responsible for removing the product from productsAvailable collection.
Keep in mind that it also has to "put back" any products not being used on update.
ngOptions documentation

AngularJS: Select option is not selected by default

Hi My current scenario is this: I want a default value too that should select.. Even if select is selected then also acceptable.. but it is blank at first..My code is this
<select class="form-control" ng-options="tsk.value for tsk in task.dropdown_values track by tsk.id" ng-model="selectedItem" ng-change="checkvalue(task.id, selectedItem)" style="width:100%;margin-right:4%;">
</select>
I'ave used this code too but not working :(
<select class="form-control" ng-options="tsk.value for tsk in task.dropdown_values track by tsk.id" ng-model="selectedItem" ng-change="checkvalue(task.id, selectedItem)" style="width:100%;margin-right:4%;">
<option selected="selected">Select</option>
</select>
Checkout my code please.
You can modify your code like this :
<select class="form-control" ng-options="tsk.value for tsk in task.dropdown_values track by tsk.id" ng-model="selectedItem" ng-change="checkvalue(task.id, selectedItem)" style="width:100%;margin-right:4%;">
<option value="">Select</option>
</select>
here is the plnkr : http://plnkr.co/edit/fekkRctRM59ydI6zDnpY?p=preview
or you can use ng-init like as mentioned by #zsong
<select class="form-control" ng-options="tsk.value for tsk in task.dropdown_values track by tsk.id" ng-model="selectedItem" ng-init="selectedItem='YourDefaultValue'" ng-change="checkvalue(task.id, selectedItem)" style="width:100%;margin-right:4%;">
</select>
Or you can look at this
var myApp = angular.module('myApp', []);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<select id="myselection" ng-init="selectedColors=3" ng-model="selectedColors">
<option value="1">Red</option>
<option value="2">Blue</option>
<option value="3">Green</option>
</select>
<div>Selected Colors: {{selectedColors }}</div>
</div>
Code for to displays by default Select options
<select ng-model="your model" ng-options="your options">
<option selected="selected" value="">Select options</option>
</select>
Your ngModel is selectedItem, so you have to set selectedItem to the value which you want as default in your controller:
//This sets the default value to the first item in your list
$scope.selectedItem = $scope.task.dropdownvalues[0].value
You should not have an option tag if using an ng-option
utilize ng-model instead as below.
<select class="form-control" ng-options="tsk.value for tsk in task.dropdown_values track by tsk.id" ng-model="selectedItem" ng-change="checkvalue(task.id, selectedItem)" style="width:100%;margin-right:4%;">
in your controller
$scope.selectedItem = dropdown_values[0];
will resolve your issue.
Many times I have spend hours to find out the solution, believe me!
[ 1 ] Select Option with Numbers (Used in Pagination, Page Size Selection)
<select id="select_pagination_pages" ng-model="recordsInPage">
<option value="10" selected>10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
<option value="150">150</option>
<option value="200">200</option>
<option value="250">250</option>
</select>
Inside Controller, particular page init function, I have inserted 10 as String.
$scope.recordsInPage = "10".toString();
[ 2 ] Second Select Option is with simple yes or no selection.
<select class="rfc_text" id="select_flag" ng-model="rfc.flag">
<option value="yes" selected>YES</option>
<option value="no">NO</option>
</select>
Inside Controller initialized rfc.flag like -
$scope.rfc.flag = "yes";
[ 3 ] This third one Select Option is special and with tiny scenario, where model will only use last four option values, but the first one is selected by default.
<select id="select_timezone" ng-model="timezone_search">
<option value="" selected>Search Entire</option>
<option value="CountryCode">Country Code</option>
<option value="CountryName">Country Name</option>
<option value="TimeZone">Time Zone</option>
<option value="TimeOffset">Time Offset</option>
</select>
Inside page controller init -
$scope.timezone_search = "";
I think three examples would be enough for peoples. :D