cannot set option of the select using angular js code? - angularjs

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.

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 ???

How to display selected name in <p> from a Select dropdown and send selected id to controller in AngularJS?

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>

Populate a select with ajax with ng-options using AngularJs

With angularJS, I need populate a select with data coming from a ajax call.
I have a controlller called "TabPacProfissionalController".In this controller I do a ajax call to get json data('profissionais object').
So far so good.
My problem is that I get the json data returning from server, but my select is never populate.
What am I missing here?
My ajax return is:
{"results":[{"nr":"8","nome":"AAAAAAAAAAAA"},
{"nr":"17","nome":"BBBBBBB"},
{"nr":"27","nome":"BBBBBAAAAA"},
,{"nr":"1004","nome":"CCCCCCCCC"}]}
HTML
<div class="form-group">
<label for="paciente.texto.sexo" class="col-sm-2 control-label">Profissional:</label>
<div class="col-sm-10" ng-controller="TabPacProfissionalController as tabProfCtrl">
<select class="form-control" ng-model="selectedProf" ng-options="nome for (nr,nome) in tabProfCtrl.profissionais">
<option value=''>Select an option</option>
</select>
</div>
</div>
JS:
app.controller('TabPacProfissionalController', function($http) {
this.profissionais = {};
$http.get('/getStaff?tipoProf=1').then(function(response){
this.profissionais=response.data.results;
console.log(this.profissionais.toString());
},function(error){
console.log(error);
});
});
If you want the label to display the nome property and the value to be the nr property try the following:
<div class="form-group">
<label for="paciente.texto.sexo" class="col-sm-2 control-label">Profissional:</label>
<div class="col-sm-10" ng-controller="TabPacProfissionalController as tabProfCtrl">
<select ng-model="tabProfCtrl.selectedProf" ng-options="profissionais.nr as profissionais.nome for profissionais in tabProfCtrl.profissionais">
<option value="">Select</option>
</select>
</div>
</div>
(key,value) notation you are using is for object data sources, as in if you wanted to iterate through the properties of a single object, rather than a collection of objects which is what you seem to be getting from the $http call.
To see the parsed server response, you could use angular.fromJson(response.data)
Here is a plunker demonstrating the functionality.

how to set the option of select dynamically via 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

angularjs how to get attribute value

I'm new to angularjs and here is a dumb question
View:
<select id="Select1" ng-model="tld" class="form-control ">
<option value="net" data-id="2">.net</option>
<option value="vn" data-id="3">.vn</option>
</select>
<input type="button" class="btn btn-test" ng-click="checkDomain(newDomain,tld)" value="CheckDomain">
On controller:
I can get the tld value(net or vn) fine, but how can i get the data-id associated with the clicked item in Select ?
Most of the time I have an array bound to the select box, seems like a better approach. But, you could try to have a json object as the value argument, like <option value="{ text: 'net', id: 2 }">.net</option>.

Resources