how to set the option of select dynamically via angularjs? - angularjs

I found many question regarding to this topic and solutions to can not help; I have select like
<select type="text" id="role" name="role" ng-model="role" ng-options="rol as rol.title for rol in rolelist" class="form-control" required>
<option value="">Select Role</option>
</select>
<p class="ng-invalid" ng-show="addForm.role.$error.required">Role need to selected</p>
and want to set the value of role model via angular js; i have done like this;
$scope.role=$json.data.role_id;
didnot work for me?

ngOptions uses an expression to define what ngModel will be bound to. Since you have defined it as:
ng-options="rol as rol.title for rol in rolelist"
You are binding ngModel to a rol. Try this:
$scope.role=$json.data

Related

add a Static option with ng-options angularJS

i ust use ng-options in a select element in angular js. and in select select i just wants to add a extra options at the beginning of options.my code ===>
<div class="form-group">
<select class="form-control select2" ng-options="(o.fullName+' ('+o.email+')') for o in SubTeamUserList track by o.id" name="SelectedUserList" ng-model="SelectedUserList" multiple data-placeholder="Search By User" style="width: 100%;">
<option value="" disabled>Select A User</option>
</select>
</div>
that is not working for me ? i dont know why.then i search on net and found some solution like =>
angularJS - add a Static option with ng-options
Add two extra options to a select list with ngOptions on it
Angular ng-options - Is there a way to add an option when the model contains a value not present in the options list?
after see above solutions i tried =>
<div class="form-group">
<select class="form-control select2" ng-options="" name="SelectedUserList" ng-model="SelectedUserList" multiple data-placeholder="Search By User" style="width: 100%;">
<option value="" disabled>Select A User</option>
<option ng-repeat="(o.fullName+' ('+o.email+')') for o in SubTeamUserList track by o.id">{{(o.fullName+' ('+o.email+')')}}</option>
</select>
</div>
after doing this this is also showing something like =>
but that is also not working for me i dont know why? can anybody help me ???

cannot set option of the select using angular js code?

I had following html
<div class="form-group ">
<label class="col-sm-3 control-label">Role</label>
<div class=" col-sm-8">
<select type="text" id="role" name="role" ng-model="role" class="form-control" required>
<option value="">Select Role</option>
<option ng-repeat="rol in rolelist" value="{{rol.id}}">{{rol.title}}</option>
</select>
<p class="ng-invalid" ng-show="addForm.role.$error.required">Role need to selected</p>
</div>
</div>
I want to set value of role dynamically clicking data-sets for the purpose of update and setting value of dom element (control); to do so i had following code inside controller
$scope.data_set=function(id)
{
BlockUi();
url=siteurl+'/admin/'+module+'/get-info';
$http({
url : url,
method : "POST",
data : {"id":id}
}).then(function(responseText){
data=responseText.data;
$scope.first_name=data.first_name;
$scope.user_id=data.id;
$scope.last_name=data.last_name;
$scope.user_name=data.user_name;
$scope.role=data.role;
$scope.email=data.email;
$scope.contact_number=data.contact;
$scope.image_file=data.image;
$scope.status=data.status;
UnblockUi();
},function(error){
UnblockUi();
UnknownError();
alert(JSON.stringify(error));
});
}
above code works for all but role model; I watched and follow other question's solutions but did not work for me?
and ng-required error is removed after this code;
May I suggest you look into the ng-options directive? You'll want to do something along the lines of:
<select ng-model="role" ng-options="rol as rol.Title for rol in rolelist">
<option value="">Select Role</option>
</select>
thanks for effort of everybody
Actually i strayed every where, but solution is simple ; its problem with datatype; actually select: role model contains string datatype and always ; i am setting the integer datatype to role model which is wrong; i convert the datatype of the data of json to string and set the value of role model works fine.

How to bind the Id of a select box selection

I have a select box that displays GeoAreaName, I need the GeoAreaId of that selection to populate a input field. I have done something similar with a typeahead input but I am unable to apply it here.
plunkr
<label>Geo Area:</label>
<select ng-controller="JobMiscCtrl" ng-model="currentItem.GeoAreaName" ng-options="job.GeoAreaName for job in geoAreaArray">
<option value=""></option>
</select>
<input type="number" ng-model="currentItem.GeoAreaId" />
I have edited your plunker. http://plnkr.co/edit/Ms3qPcwj6dxVF3y75ljZ?p=preview
You had 2 problems.
Your input field was out of the scope of your ng-controller declaration
Your ng-options expression needed to be slightly different.
<select
ng-model="currentItem"
ng-options="job.GeoAreaId as job.GeoAreaName for job in geoAreaArray">
<option value=""></option>
</select>

<select> placeholder with angular/bootstrap not working

I would like to have a select with a placeholder in angularjs with bootstrap. The solutions I have found do not work with angular
Here is my angular markup:
<select ng-model="myModel"
ng-options="p.name for p in ..."
class="form-control list-selector required">
<option value='' disabled selected>Please Choose</option>
</select>
If someone has a working solution...
thanks :)
You need to add an empty option to your select:
<option value="">- Please Choose -</option>
Here is a working example: http://jsfiddle.net/DianaNassar/FShdc/
For Angularjs Versions and reactive forms , if you are using ngValue to bind values in options , the following type of solution will work:
<option value='' selected ng-value="null">Please Choose</option>
For Angular 2+ Versions something like this will work-
<select class="form-control"
formControlName="roomType"
(change)="changeRoomType($event)">
<option [ngValue]="null" selected>Room Type</option>
<option *ngFor="let room of roomTypes" [ngValue]="room">{{room}}</option>
</select>
For reference, check this link out-https://netbasal.com/angular-quick-tip-how-to-show-a-placeholder-in-select-control-bab688f98b98

How to set the value attribute for select options?

Source JSON data is:
[
{"name":"Alabama","code":"AL"},
{"name":"Alaska","code":"AK"},
{"name":"American Samoa","code":"AS"},
...
]
I try
ng-options="i.code as i.name for i in regions"
but am getting:
<option value="?" selected="selected"></option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
while I am expecting to get:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AS">American Samoa</option>
So, how to get value attributes and get rid of "?" item?
By the way, if I set the $scope.regions to a static JSON instead of AJAX request's result, the empty item disappears.
What you first tried should work, but the HTML is not what we would expect. I added an option to handle the initial "no item selected" case:
<select ng-options="region.code as region.name for region in regions" ng-model="region">
<option style="display:none" value="">select a region</option>
</select>
<br>selected: {{region}}
The above generates this HTML:
<select ng-options="..." ng-model="region" class="...">
<option style="display:none" value class>select a region</option>
<option value="0">Alabama</option>
<option value="1">Alaska</option>
<option value="2">American Samoa</option>
</select>
Fiddle
Even though Angular uses numeric integers for the value, the model (i.e., $scope.region) will be set to AL, AK, or AS, as desired. (The numeric value is used by Angular to lookup the correct array entry when an option is selected from the list.)
This may be confusing when first learning how Angular implements its "select" directive.
You can't really do this unless you build them yourself in an ng-repeat.
<select ng-model="foo">
<option ng-repeat="item in items" value="{{item.code}}">{{item.name}}</option>
</select>
BUT... it's probably not worth it. It's better to leave it function as designed and let Angular handle the inner workings. Angular uses the index this way so you can actually use an entire object as a value. So you can use a drop down binding to select a whole value rather than just a string, which is pretty awesome:
<select ng-model="foo" ng-options="item as item.name for item in items"></select>
{{foo | json}}
If you use the track by option, the value attribute is correctly written, e.g.:
<div ng-init="a = [{label: 'one', value: 15}, {label: 'two', value: 20}]">
<select ng-model="foo" ng-options="x for x in a track by x.value"/>
</div>
produces:
<select>
<option value="" selected="selected"></option>
<option value="15">one</option>
<option value="20">two</option>
</select>
If the model specified for the drop down does not exist then angular will generate an empty options element. So you will have to explicitly specify the model on the select like this:
<select ng-model="regions[index]" ng-options="....">
Refer to the following as it has been answered before:
Why does AngularJS include an empty option in select? and this fiddle
Update: Try this instead:
<select ng-model="regions[index].code" ng-options="i.code as i.name for i in regions">
</select>
or
<select ng-model="regions[2]" ng-options="r.name for r in regions">
</select>
Note that there is no empty options element in the select.
You could modify you model to look like this:
$scope.options = {
"AL" : "Alabama",
"AK" : "Alaska",
"AS" : "American Samoa"
};
Then use
<select ng-options="k as v for (k,v) in options"></select>
It appears it's not possible to actually use the "value" of a select in any meaningful way as a normal HTML form element and also hook it up to Angular in the approved way with ng-options. As a compromise, I ended up having to put a hidden input alongside my select and have it track the same model as my select, like this (all very much simplified from real production code for brevity):
HTML:
<select ng-model="profile" ng-options="o.id as o.name for o in profiles" name="something_i_dont_care_about">
</select>
<input name="profile_id" type="text" style="margin-left:-10000px;" ng-model="profile"/>
Javascript:
App.controller('ConnectCtrl',function ConnectCtrl($scope) {
$scope.profiles = [{id:'xyz', name:'a profile'},{id:'abc', name:'another profile'}];
$scope.profile = -1;
}
Then, in my server-side code I just looked for params[:profile_id] (this happened to be a Rails app, but the same principle applies anywhere). Because the hidden input tracks the same model as the select, they stay in sync automagically (no additional javascript necessary). This is the cool part of Angular. It almost makes up for what it does to the value attribute as a side effect.
Interestingly, I found this technique only worked with input tags that were not hidden (which is why I had to use the margin-left:-10000px; trick to move the input off the page). These two variations did not work:
<input name="profile_id" type="text" style="display:none;" ng-model="profile"/>
and
<input name="profile_id" type="hidden" ng-model="profile"/>
I feel like that must mean I'm missing something. It seems too weird for it to be a problem with Angular.
you can use
state.name for state in states track by state.code
Where states in the JSON array, state is the variable name for each object in the array.
Hope this helps
Try it as below:
var scope = $(this).scope();
alert(JSON.stringify(scope.model.options[$('#selOptions').val()].value));

Resources