I'm almost there but stuck on one little detail. I can't seem to get the "name" property of the selected Car Make's "Car Model" to actually show up on the select menu. At the moment, it seems to be showing the correct number of options, but the options are visibly BLANK. Here's my code to help better explain...
$scope.carMakes = [
{
name: 'Honda',
models: [{name:'Accord'}, {name: 'Civic'}, {name: 'CRX'}]
},
{
name: 'Toyota',
models: [{name:'Camry'}, {name: 'Forerunner'}]
}
];
<div class="input-group">
<label>Vehicle Make</label>
<select id="carMake" ng-model="carMake" ng-options="carMake as carMake.name for carMake in carMakes track by carMake.name">
<option value="">All</option>
</select>
</div>
<div class="input-group carModels">
<label>Vehicle Model</label>
<select id="carModel" ng-model="carMake.model" ng-options="carMake.model as carMake.model.name for carMake in carMake.models">
<option value="">All</option>
</select>
</div>
Can someone please tell me why even though the carModel select options show up, the "name" isn't being displayed? Thank you!
You want something like this:
<select id="carModel" ng-model="carMake.model" ng-options="carModel as carModel.name for carModel in carMake.models">
<option value="">All</option>
</select>
We're using carModel.name as the display and setting the value as the object itself. So in your controller, $scope.carMake.model will be equal to the model object. So if the user selects "Accord" then in your controller:
$scope.carMake.model.name === 'Accord' // True
would evaluate to true after the selection is made.
Related
I have the following select input in my html which is populated using ng-options. I want to show the selected NAME down below in whereas I want to send the selected ID back to the controller. I get the required id from ng-model="user.category". How can I show the selected name? I also show the names in options.
<select ng-model="user.category" ng-options="category.id as category.name for category in categories" class="form-control" class="select" required>
<option value="{{category.name}}">Select a Category</option>
</select>
<p>Available On : {{user.retailerBranchId}}</p>
<select ng-model="user.category" ng-options="category for category in categories" class="form-control" class="select" required>
<option value="{{category.name}}">Select a Category</option>
</select>
<p>Available On : {{user.category.id}}</p>
If you make retailerBranchId a method you can do it with something like lodash' _.find() (or even native find). You have user.category updating according to your selection, which you say has the id in it, which you can use:
function retailerBranchId() {
return _.get(_.find(this.categories, { id: user.category }), 'name', '');
}
This also fails gracefully; if it can't find any name, or any item that matches the id, it'll return an empty string.
Edit: You would call it in a similar fashion like so:
<p>Available on: {{ user.retailerBranchId() }}</p>
Although really, it's better to use it like this:
<p data-ng-bind="'Available on: ' + user.retailerBranchId()"></p>
Can you try like a below method,
Controller to get selected Category Name:
$scope.getSelectedCategoryDetail=function(selectedCat){
angular.forEach($scope.categories, function(cat){
if(selectedCat===cat.id){
$scope.user.name=cat.name;
}
});
};
Template to have ng-change event:
<select ng-model="user.category" ng-options="category.id as category.name for category in categories" class="form-control" class="select" required>
<option value="{{category.name}}" ng-change="getSelectedCategoryDetail(user.category)">Select a Category</option>
</select>
<p>Category Id : {{user.category}}</p>
<p>Category Name : {{user.name}}</p>
i have a very simple problem :
Here is my object inside my controller :
$scope.footballeur = {'identifiant':6,'prenom':'Thierry','nom':'Chalamerto','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':'28'};
Please notice the int variable categorie .
Now in my view, i 've got a VERY SIMPLE select like this :
<div class="input-group">
<span class="input-group-addon stdgp"></i> Catégorie</span>
<select ng-init="footballeur.categorie" ng-model="footballeur.categorie" class="form-control" >
<option value="1">Junior</option>
<option value="2">Confirmé</option>
</select>
</div><br>
Then, believe me or not, but the select is unable to get setted by my value.
It shows a blank line , it is really annoying.
I would really appreciate if somebody has an idea, the only thing who works is removing ng-model and replacing it by ng-value, but then, i can't save the new values chosen by the user !
I really dont know what to do, i 've tried ng-value instead of value, it still doesn't work !! I've removed ng-init, it changes nothing still ! there is not even a loop inside my SELECT , it is supposed to work directly !
here is a pic of the horrible situation :
NEW ANSWER (sorry I misunderstood your question at first glance)
I think your problem has to do with "selected" attribute. Try something like this:
<option ng-selected="footballeur.categorie == 1" value="1">Junior</option>
OLD ANSWER
Make the select like this
<select ng-model="footballeur_selected" class="form-control">
<option ng-repeat="(fname, fvalue) in footballeur" value="{{fvalue}}">{{fname}}</option>
</select>
Look into using ng-options to set the <select> options.
Example (HTML):
<select class='form-control'
ng-model="mySelectedOption"
ng-options="myOptions">
</select>
Example (JS):
$scope.myOptions = [
{ id: "option1", name: "Option 1" },
{ id: "option2", name: "Option 2" },
{ id: "option3", name: "Option 3" }
];
It works automatic !
ng-init does not work that way. It is simple used to evaluate an expression.
Your code works fine even without ng-init.
<select ng-model="footballeur.categorie" class="form-control" >
<option value="1">Junior</option>
<option value="2">Confirmé</option>
</select>
Check out the implementation here.
I've a quick issue that I can't handle.
I've a data for a dropdown, and depending on the value that's being picked for that dropdown, I want the data to change on the second dropdown, also preferably if it's hidden until the first dropdown is picked.
Atm, I can pick the first controller but I don't have an idea on how to connect the two dropdowns.
Any help will be greatly appreciated. Thank you in advance
new test service
export default class newtest {
constructor($http){
'ngInject';
this._$http = $http;
let testData = this;
this.options = {
releases: [
{value: 1, name: 'R1', environments : ['R1-QA1', 'R1-QA2']},
{value: 2, name: 'R2', environments : ['R2-QA1', 'R2-QA2']},
{value: 3, name: 'R3', environments : ['R3-QA1', 'R3-QA2']}
],
environments : [
['R1-QA1', 'R1-QA2'],
['R2-QA1', 'R2-QA2'],
['R3-QA1', 'R3-QA2']
]
};
}
}
new test controller
class NewTestCtrl {
constructor(NewTest, $state, $http) {
'ngInject';
this._NewTest = NewTest;
this.options = this._NewTest.options;
this.releaseValues = this.options.releases.name;
this.envValues = [];
}
HTML
<fieldset>
<select ng-model="release" ng-options="release.name for release in $ctrl.options.releases">
<option value="" disabled selected>Select</option>
<option value="value">{{name}}</option>
</select>
</fieldset>
<fieldset>
<select ng-model="release" ng-options="env for environments in $ctrl.options.environments">
<option value="" disabled selected>Select</option>
<option value="env">{{env}}</option>
</select>
</fieldset>
First, both your select elements have ng-model="release". This is the data field it is bound to in your controller. The second select element should be something like ng-model="environment".
You should be able to make the environment list change based on the the release selected by having the options use the release data element, something like ng-options="env for environments in $ctrl.options.releases[release].environments"
Lastly, you could hide the environment select until a release is selected with a ng-Show attribute based on release having a value. Something like ng-Show="release>0" and initialize release to 0 in your constructor.
Thanks for the help #Jim Moore, I found a good fiddle link that I followed for guidelines https://jsfiddle.net/annavester/Zd6uX/
And I changed the json of my data to >
environments : {
'Release 1' : ['R1-QA1', 'R1-QA2'],
'Release 2' : ['R2-QA1', 'R2-QA2'],
'Release 3' : ['R3-QA1', 'R3-QA2']
}
and also the html to, For anyone going through the same issue in the future, I hope this helps.
<fieldset>
<select id="release"
ng-model="$ctrl.release"
ng-hide="!$ctrl.browsers"
ng-options="release for (release, environments) in $ctrl.environments">
<option value="" disabled selected>Select</option>
</select>
</fieldset>
<fieldset>
<select id="environment"
ng-model="$ctrl.environment"
ng-hide="!$ctrl.release"
ng-options="environment for (release, environment) in $ctrl.release">
<option value="" disabled selected>Select</option>
</select>
</fieldset>
I have the following Object in TypeScript defining some options for a <select> using ng-options:
$scope.sOptions = [
{
name: "Female"
},
{
name: "Male"
}];
I provide the following for ng-options to AngularJS:
ng-options="opt.name for opt in sOptions"
This actually works except for the fact that according to the documentation for ng-option, the value should just be the loop counter while building out the <option> elements. However I see the following rendered:
<option value="?" selected="selected"></option>
<option label="Female" value="object:6">Female</option>
<option label="Male" value="object:7">Male</option>
What I'm expecting to see is something like the following:
<option value="0" selected="selected"></option>
<option label="Female" value="1">Female</option>
<option label="Male" value="2">Male</option>
What am I doing incorrectly to have those odd values being produced for the value of the <option> element?
EDIT: After reading the correct answer provided, this other post goes into detail about a similar issue and also how to use track by for this requirement: https://stackoverflow.com/a/30292209/410937
In order to achieve this requirement you have to use track by in ng-select. The track by will help you in binding the select option with a value tag. You should also provide an Unique Id field to track the select option.
<select ng-model="selectedName" ng-options="item.Name for item in names track by item.Id">
</select>
In your controller the names object will be like this
$scope.names = [{Name: "Name1", Id: 0}, {Name: "Name2", Id: 1}, {Name: "Name3", Id: 2}];
In order to initialize the select option you can set the model by
$scope.selectedName = $scope.names[0];
You can initialize the scope variable only if you use the track by property in select option
This happens because you haven't used an ng-model in your select attribute. Angular does this to prevent model select by accident. You need to bind your select and the "empty" option will go away.
<select ng-options="opt.name for opt in sOptions"
ng-model="someScopeVar">
</select>
For example I would like to do something like this:
<select ng-model="persons">
<option value="person1">Anna Smith</option>
<option value="person2">Karl Pettersson</option>
<option value="person3">Ylvis Russo</option>
</select>
<p ng-view="persons"><p>
And having the view display each name when selected in the dropdown rather than it's value. Is it possible?
I tried watching the model and assigning a new model the text value with jQuery. However it ended up being complicated so if that's the best way to do it, I small example would be awesome!
You just need to define your persons object and then you can do whatever you want with it. There are many ways to do it... Here's an example:
HTML
<select ng-model="persons"
ng-options="p as p.label for p in persons">
</select>
<p ng-repeat="p in persons">
{{p.value}}: {{p.label}}
</p>
JS
$scope.persons = [
{ value: 'person1', label: 'Anna Smith' },
{ value: 'person2', label: 'Karl Pettersson' },
{ value: 'person3', label: 'Ylvis Russo' }
];
Here is a jsfiddle: http://jsfiddle.net/bKHh8/
UPDATE
Here it is with option tags which don't use angular indices for values (this is exactly what answers your question): http://jsfiddle.net/bKHh8/1/
<select>
<option ng-repeat="p in persons" value="{{p.value}}">{{p.name}}</option>
</select>