AngularJS dynamic ng-options - angularjs

I have
select
<select ng-model="country" ng-options="{{selectOptions}}">
<option style="display:none" value="">Select country</option>
</select>
and json country list {id :..., nameEn: ..., nameRu: ...}
Can someone explain why this works:
$scope.lang = "En";
$scope.selectOptions = "country as country['name'+lang] for country in countries";
$scope.customerChanged = function(v){
if (/[а-яА-ЯЁё]/.test(v))
$scope.lang = "Ru";
else
$scope.lang = "En";
}
But this doesn't:
$scope.selectOptions = "country as country['nameEn'] for country in countries";
$scope.customerChanged = function(v){
if (/[а-яА-ЯЁё]/.test(v))
$scope.selectOptions = "country as country['nameRu'] for country in countries";
else
$scope.selectOptions = "country as country['nameEn'] for country in countries";
}
This doesn't work either:
<select ng-model="country" ng-options="country as country.name{{lang}} for country in countries">
<option style="display:none" value="">Select country</option>
</select>
$scope.lang = "En";
$scope.customerChanged = function(v){
if (/[а-яА-ЯЁё]/.test(v))
$scope.lang = "Ru";
else
$scope.lang = "En";
}

<!--First Solution-->
<select ng-model="selectValue" ng-options="item.Name for item in items">
<option selected value="">Select Value</option>
</select>
<!--Second Solution-->
<select ng-model="selectValue">
<option value="{{item.Value}}" ng-repeat="item in items">
{{item.Name}}
</option>
</select>
<!--Third Solution-->
<select ng-model="selectValue" ng-options="item.Name for item in items">
</select>
<!--For Multiple Select-->
<select ng-model="" multiple ng-options="">
</select>

From angular documentation:
Why mixing interpolation and expressions is bad practice:
There is no guarantee that it works for every directive, because
interpolation itself is a directive. If another directive accesses
attribute data before interpolation has run, it will get the raw
interpolation markup and not data.

Related

AngularJS - default value in selectbox

I have a problem with default value in select element.
I have form for create new item and update from table.
Here is my code:
Controller:
function MyController($scope){
$scope.data = {};
$scope.selectedItem = null; //selected row from table
this.edit = function(){
$scope.enableEditation = true; // displayed editation form
$scope.data = $scope.selectedItem
}
}
HTML:
<select required ng-model="data.ID" class="form-control">
<option ng-repeat="option in types" value="{{option.ID}}" ng-selected="option.ID == data.ID">{{option.ID}} - {{option.NAME}}</option>
</select>
Default value is allways empty. Generated html in browser is:
<select class="form-control ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required" required="" ng-model="data.ID">
<option selected="selected" value="? number:4 ?"></option>
<option class="ng-binding ng-scope" value="4" ng-repeat="option in types" ng-selected="option.ID == data.ID">4 - First</option>
<option class="ng-binding ng-scope" value="12" ng-repeat="option in types" ng-selected="option.ID == data.ID">12 - Last</option>
</select>
Where can be problem? Thanks for advices.
Your problem is that your adding the value to the options trough
"value={{option.id}}"
This will always result in type conflict when your working with non-string values, since doing so will convert your id to a string, your simplest solution is to use "ng-value" instead and give it as reference:
"ng-value="option.id"
You just replace ng-selected="option.ID == data.ID" to ng-selected="data.ID"
<select required ng-model="data.ID" class="form-control">
<option ng-repeat="option in types" value="{{option.ID}}" ng-selected="data.ID">{{option.ID}} - {{option.NAME}}</option>
</select>
See the document https://docs.angularjs.org/api/ng/directive/ngSelected

Select by name and get the Id of the selected item with Angular

I am working in an MVC project with HTML and angular.
I have a select list which get selected based on a name entered in an input box, but I am not able to get the id.
This works but I don't get the Id of the selected item:
<input type="text" ng-model="selectedCountry">
<select class="form-control"
ng-model="selectedCountry"
ng-options="country.name as country.name for country in countries">
<option value="">Select Country</option>
And this is not working, as I am not able to select by the name and get the id:
<input type="text" ng-model="selectedCountry">
<select class="form-control"
ng-model="selectedCountry"
ng-options="country.id as country.name for country in countries">
<option value="">Select Country</option>
Here is the plunker: Plunker
Thanks for the help!
<select class="form-control" data-ng-change="call(country.id)"
ng-model="country.id"
ng-options="country.id as country.name for country in countries">
<option value="">Select Country</option>
</select>
In order for the second scenario to work you'll need to get the id of the country in your controller like this:
$scope.selectedCountry = $scope.countries.find(function(country) {
return country.name === "Italy";
}).id;
try this
html
<input type="text" ng-model="selectedCountryId">
<select class="form-control" ng-model="selectedCountryId">
<option ng-repeat="country in countries" value="{{country.id}}">{{country.name}}</option>
</select>
<div>selected country id: {{selectedCountryId}} </div>
in controller add $scope.selectedCountryId = "";
hope it helps
Or
<input type="text" ng-model="selectedCountry.name">
<select class="form-control"
ng-model="selectedCountry"
ng-options="country as country.name for country in countries">
<option value="">Select Country</option>
</select>
In your HTML
and
$scope.selectedCountry = {};
in your javascript
I don't think that's possible when you use the same model on different select which are using different keys for the model.
I would go with using the name in both models and then fetching the id from the array:
$scope.getId = function(){
var co = $scope.countries.filter(function (el) { return el.name == $scope.selectedCountry });
if(typeof co !== 'undefined' && co.length > 0){
$scope.selectedId =co[0].id;
}else{
$scope.selectedId = null;
}
};
plunker here

Set ng-options value based on string AJAX value

I have two select menus that are populated via AJAX JSON data fetched after page load. They are linked by their own model and look like so;
<select name="category"
id="category"
ng-model="category"
ng-options="category as category.name for category in categories track by category.name">
<option value=''>Select category</option>
</select>
<select name="product"
id="product"
ng-disabled="!category"
ng-model="product"
ng-options="product for product in category.products">
<option value=''>Select product</option>
</select>
After the form is submitted to the API, it returns an originobject consisting of both the category and product strings if they are used.
I thought I should then be able to set the $scope.category and/or $scope.product to the returned origin data to pre-select the item in the select menu/s;
.success(function(data)) {
...
$scope.category = data.origin.category
...
}
But this does not work...
You can use ng-init to select initial value
<select ng-init="initDefaultCategory(categories)" name="category" id="category" ng-model="category" ng-options="category as category.name for category in categories track by category.name">
<option value=''>Select category</option>
</select>
<select ng-init="initDefaultProduct(category.products)" name="product" id="product" ng-disabled="!category" ng-model="product" ng-options="product for product in category.products">
<option value=''>Select product</option>
</select>
Working DEMO
EDITS
If you want to select default value, you can do something like this
Controller
$scope.initDefaultCategory = function(categories, value) {
if (categories.length > 0)
$scope.category = categories.filter(e => e.name == value)[0];
}
And you can pass value in ng-init
<select ng-init="initDefaultCategory(categories,'All')" name="category" id="category" ng-model="category" ng-options="category as category.name for category in categories track by category.name">
<option value=''>Select category</option>
</select>
Edited Demo
.success(function(data)) {
$scope.category = $scope.categories.filter(function(val,key){
return data.origin.category['yourKey']==val['yourKey']
})[0]
}

AngularJS - Dynamic <option>s not being properly selected (by default)

In my case, the dynamic (provided or created through ng-repeat) <option>s in a <select> are not properly selected (like in cases when the ng-model has an init value).
<select ng-model="my-model">
<option ng-repeat="n in numbers" value={{ n.val }}>{{ n.name }}</option>
</select>
Here's a plnkr to demonstrate my problem.
Try to use the ng-options directive on the select element instead of doing an ng-repeat.
<select ng-model="model2" ng-init="model2 = '3'" ng-options="n.value as n.name for n in numbers">
<option value="">Select an option..</option>
</select>
instead of
<select ng-model="model2" ng-init="model2 = '3'">
<option value="">Select an option..</option>
<option ng-repeat="n in numbers" value="{{ n.value }}"> {{ n.name }} </option>
</select>
source: select directive doc
updated plunkr: ng-options plunkr
EDIT:
If you really want to keep your ng-repeat, you can add additional markup to make it work:
<select ng-model="model2" ng-init="model2 = '3'">
<option value="">Select an option..</option>
<option ng-repeat="n in numbers" value="{{ n.value }}" ng-selected="n.value == model2"> {{ n.name }} </option>
</select>
updated plunkr: ng-repeat plunkr
Regards,
Camusensei

selected option into ng-repeat

I'm having the following script,
<select ng-model="create.Category" class="form-control input-sm" name="category" required>
<option value="" disabled="" selected="">Select Category</option>
<option ng-repeat="cat in categories" value="{{ cat.type }}">{{cat.type}}</option>
</select>
so my controller looks like this (categories is a factory which reaturn my categories to fill the select options, and info is an object instance with the parameters):
app.controller('myCtrl', function($scope, categories, info) {
$scope.categories = categories;
$scope.create = info;
});
so, my select options are filled perfectly, however, the ng-model="create.category" which has a value, is not been selected, it display the first category on the list.
Any guessing?
use ng-selected in select and compare the cat.type with value given
<select ng-model="create.Category" class="form-control input-sm" name="category" required>
<option value="" disabled="" selected="">Select Category</option>
<option
ng-selected="{{create.Category == cat.type}}"
ng-repeat="cat in categories" value="{{ cat.type }}">{{cat.type}}</option>
</select>
The preferred way is to use ngOptions :
https://docs.angularjs.org/api/ng/directive/ngOptions

Resources