I have a drop down that should cause a data fetch on change. I don't need two way binding to a model for the drop down. I just want it initially populated with a list of departments and when a user selects one, it gets the list of users in that department.
The select looks like this:
<select class="form-control" id="selDepartmentList" ng-model="departmentList" ng-change="getUsersInDepartment(document.getElementById("selDepartmentList").options[document.getElementById("selDepartmentList").selectedIndex].value)">
<option value="-1">All</option>
<option ng-repeat="dept in departmentList"
value="{{dept.DepartmentId}}">
{{dept.DepartmentName}}
</option>
</select>
I tried ng-change without ng-model, but it fails since ng-change requires ng-model for some reason. I tried setting ng-model to null and empty string, but neither worked. I also tried not using ng-change at all and using onchange, but getUsersInDepartment can't be found through onchange since it's attached to my controller. With ng-model set to departmentList, the drop down won't hold a value, any selection is erased.
All I want to have happen is that when a user selects a department it passes the id for that department to getUsersInDepartment, which will then fetch the user list. But right now getUsersInDepartment is never called.
departmentList is defined in my controller and attached to $scope. All the examples I've seen have some kind of selectedModelObject that they bind to the drop down. I don't have one of those.
My controller looks like:
controller('AdminTableCtrl', function ( $scope, coreAPIservice ) {
$scope.userList = [];
$scope.departmentList = [];
coreAPIservice.GetUserList().success(function (response) {
$scope.userList = response;
});
coreAPIservice.GetDepartmentList().success(function (response) {
$scope.departmentList = response;
});
$scope.getUsersInDepartment = function(deptId) {
if(deptId === -1) {
coreAPIservice.GetUserList().success(function (response) {
$scope.userList = response;
});
}
else {
coreAPIservice.GetUsersInDepartmentList(deptId).success(function (response) {
$scope.userList = response;
});
}
}
});
Edit:
My original attempt with ng-options:
<select class="form-control" id="selDepartment"
ng-model="selectedDepartment"
ng-options="dept as dept.DepartmentName for dept in departmentList track by dept.DepartmentId">
<option value="">Select Team...</option>
</select>
selectedDepartment is defined as:
$scope.selectedDepartment = {};
The solution is to avoid decorating the <select> element with any angular directives and instead place ng-click on each <option>.
Like this:
<select class="form-control" id="selDepartmentList">
<option value="-1" selected>All</option>
<option ng-repeat="dept in departmentList"
ng-click="getUsersInDepartment(dept.DepartmentId)"
value="{{dept.DepartmentId}}">
{{dept.DepartmentName}}
</option>
</select>
Making a custom directive should work for this problem.
angular
.module('my_module')
.directive('ngCustomChange', function($parse) {
return function(scope, element, attrs) {
var fn = $parse(attrs.ngCustomChange);
element.bind('change', function(event) {
scope.$apply(function() {
event.preventDefault();
fn(scope, {$event:event});
});
});
};
});
<select ng-custom-change="$ctrl.myFunction()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
Related
I have a select tag with options populated by AngularJS. I am trying to select an option if it equals to another property in scope. Option values and scope property I am trying to compare are both coming from async http call. So there is always delay, then it is not working properly. What is the best practice to make sure that both scope property are resolved and ready to compare.
ng-selected="MusteriId == option.Value" is comparing part.
<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
<option ng-selected="MusteriId == option.Value"
ng-repeat="option in MusteriList" value="{{option.Value}}">
{{option.Text}}
</option>
</select>
This is my controller where two http calls are performed.
(function() {
var biletController = function ($scope, $http, commonFunctions) {
$scope.Id = null;
$scope.BiletNo = null;
$scope.BiletTarihi = null;
$scope.CurrencyId = null;
$scope.MusteriId = null;
$scope.PAID_EUR = null;
$scope.PAID_TL = null;
$scope.PAID_USD = null;
$scope.ServisIstiyorMu = null;
$scope.TOTAL = null;
$scope.TourId = null;
$scope.MusteriList = null;
$scope.openEditFormJS = function(e) {
$http.get('/Bilet/Get/' + e)
.then(function (response) {
console.log(response.data);
$scope.Id = response.data.Id;
$scope.BiletNo = response.data.BiletNo;
if (response.data.BiletTarihi) {
$scope.BiletTarihi = commonFunctions.formatDate(new Date(parseInt(response.data.BiletTarihi.substr(6))));
}
$scope.CurrencyId = response.data.CurrencyId;
$scope.MusteriId = response.data.MusteriId;
$scope.PAID_EUR = response.data.PAID_EUR;
$scope.PAID_TL = response.data.PAID_TL;
$scope.PAID_USD = response.data.PAID_USD;
$scope.ServisIstiyorMu = response.data.ServisIstiyorMu;
$scope.TOTAL = response.data.TOTAL;
$scope.TourId = response.data.TourId;
$('#modal').modal('show');
});
$http.get('/Bilet/GetMusteriSelectList')
.then(function (response) {
console.log(response.data);
$scope.MusteriList = response.data;
});
};
};
app.controller('BiletController', ['$scope', '$http', 'commonFunctions', biletController]);
}());
Use the ng-value directive for non-string values1
<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
<option ̶n̶g̶-̶s̶e̶l̶e̶c̶t̶e̶d̶=̶"̶M̶u̶s̶t̶e̶r̶i̶I̶d̶ ̶=̶=̶ ̶o̶p̶t̶i̶o̶n̶.̶V̶a̶l̶u̶e̶"̶
ng-repeat="option in MusteriList" ng-value="option.Value">
{{option.Text}}
</option>
</select>
For more information, see Using ngValue to bind the model to an array of objects
Don't use ngSelected with ngModel2
<select id="MusteriId" name="MusteriId" ng-model="MusteriId">
<option ̶n̶g̶-̶s̶e̶l̶e̶c̶t̶e̶d̶=̶"̶M̶u̶s̶t̶e̶r̶i̶I̶d̶ ̶=̶=̶ ̶o̶p̶t̶i̶o̶n̶.̶V̶a̶l̶u̶e̶"̶
ng-repeat="option in MusteriList" value="{{option.Value}}">
{{option.Text}}
</option>
</select>
From the Docs:
Note: ngSelected does not interact with the <select> and ngModel directives, it only sets the selected attribute on the element. If you are using ngModel on the select, you should not use ngSelected on the options, as ngModel will set the select value and selected options.
— AngularJS ng-selected API Reference
See additional Docs:
Using ng-repeat to generate select options
Using select with ng-options and setting a default value
See Stackoverflow:
Using ng-repeat to generate select options (with Demo)
I've a DropDownList where user has to select options and save it to database. I am using the following with AngularJs:
<select>
<option>----Please Select Sub-Category----</option>
<option ng-repeat="m in Categories" value="{{ m.CategoryId }}" ng-model="saveProducts.CategoryId">{{ m.Category }}</option>
</select>
I can show the values in the above DropDownList but stuck to retrieve the value from the selected and pass it to the scope. I've tried even this, a silly one:
<select>
<option>----Please Select Sub-Category----</option>
<option ng-repeat="m in Categories" value="{{ m.CategoryId }}" ng-model="m.CategoryId">{{ m.Category }}</option>
</select>
But that will not work. saveProducts is the object (scope) where I am passing values but is there any easy way where I can pass option value with the above procedure?
Here what I am doing to save data in database and it works fine except the option value, it's unable to retrieve values with the above:
productApp.controller('addProductController', function ($scope, $http) {
$scope.addData = function () {
$http({
method: 'POST',
url: '/Product/AddProductsToDb',
data: $scope.saveProducts
}).success(function () {
$scope.saveProducts = null;
}).error(function () {
alert('Failed');
});
}
});
This is the output I have and just want to pass the option value from it:
Update 1 - This is what I've tried but I can show the value in the alert method using as follows:
<select ng-model="saveProducts.ParentId"
ng-options="m.Category for m in Categories track by m.CategoryId">
<option value="">----Please Select Sub-Category----</option>
</select>
AngularJs Controller:
productApp.controller('addProductController', function ($scope, $http) {
$scope.addData = function () {
angular.forEach($scope.saveProducts, function (model, index) {
$scope.saveProducts.ParentId = (model.CategoryId);
});
alert($scope.saveProducts.ParentId);
$http({
method: 'POST',
url: '/Product/AddProductsToDb',
data: $scope.saveProducts
}).success(function () {
$scope.saveProducts = null;
}).error(function () {
alert('Failed');
});
}
});
Note: It saves TextBox input value but stuck with DropDownList. Unable to retrieve select option value and save it to database.
You should have a property to store the selected option. You can use ng-options to render the dropdown.
<select ng-model="selectedCategory"
ng-options="option.Category for option in Categories track by option.CategoryId ">
<option value="">Select Option</option>
</select>
Now your select element's ng-model is set to selectedCategory. So in your add method you can access that and use that for saving.
$scope.addData = function () {
console.log($scope.selectedCategory);
//to do : use selectedCategory
}
Use ngOptions. Depending on the structure of your Categories data, you could do something like:
<select ng-options="m as m.yourProperty for m in Categories" ng-model="selected"></select>
Then, in Angular...
$scope.selected = $scope.Categories[0];
Read the ngOptions documentation to tweak according to your needs.
I can't figure out why i can't bind to a select element
there is my code:
<select ng-model="site" ng-change="getAll()">
<option value="SG1">SG1</option>
<option value="PZ1">PZ1</option>
<option value="NE1">NE1</option>
</select>
getAll() make an alert of 'site' but the var is never updated.
$scope.site is nerver use except in getAll()
$scope.getAll = function () {
alert($scope.site);
}
If i set $scope.site to a value it is display but never update either
Edit:
I forgot a big detail...
The select is display with a ng-include directive
<section id="sectionLeft" ng-include="nav[navId]">
</section>
ng-include creates a new scope which prototypally inherits from your controller. So you are initially reading the selected option from your controller, but when the select element writes a new selected option it ends up writing to the inherited scope.
You can bind to an object instead.
Controller:
$scope.data = { site: "SG1" };
$scope.getAll = function() {
alert($scope.data.site);
}
Template:
<select ng-model="data.site" ng-change="getAll()">
<option value="SG1">SG1</option>
<option value="PZ1">PZ1</option>
<option value="NE1">NE1</option>
</select>
See this answer for more details.
If you don't like switching to an object, look up controller as syntax and bind directly to the controller instead of $scope.
I've fiddled your code and i'm able to get the updated value in the alert box.
$scope.getAll = function() {
alert($scope.site);
};
Working Fiddle
I have one select in which I bind data from database by get method.it binds data perfectly in select.but when I select one of it option .its disappears..any help appreciated.
This is my HTML Code:
<select class="form-control" ng-model="MainCategory" ng-options="main.Name for main in MainCategory track by main.ID" placeholder="Select Main Category">
<option value=""></option>
</select>
this is my contoller code
var baseURL = 'http://localhost:50928/api/ProductAPI/';
var MainCategory = [];
url = baseURL + "GetMainCategoryList";
$http.get(url)
.success(function (data) {
$scope.MainCategory = data;
console.log(data);
}).error(function (data) {
console.log(data);
});
Your ng-options data is MainCategory and your ng-model is binded to it as well. This means that selecting an options turns your data to one value only - your selected option. In your case you should have a data property, lets say - categories.
Like this:
ng-options="main.Name for main in categories track by main.ID"
In addition you will hold in your controller another property for the selected Category and ng-model will bind to it:
ng-model="selectedCategoty"
Trying to select multiple options in angularjs regarding to object values
Here is a code:
myapp.controller('myctrl', [
'$scope',
function ($scope) {
$scope.query = {
Statuses: {
Draft: true,
Live: true,
Pending: true,
Archived: false,
Deleted: false
}
};
}
]);
And html
<div ng-controller="myctrl">
<select multiple>
<option value="Draft" ng:model="query.Statuses['Draft']">Draft</option>
<option value="Pending" ng:model="query.Statuses.Pending">Pending</option>
<option value="Live" ng:model="query.Statuses.Live">Live</option>
<option value="Archived" ng:model="query.Statuses.Archived">Archived</option>
<option value="Deleted" ng:model="query.Statuses.Deleted">Deleted</option>
</select>
{{query | json}}
</div>
(Non)working sample on jsfiddle
http://jsfiddle.net/andrejkaurin/h9fgK/
Using a model for statuses ($scope.statuses), and ng-options to iterate over them:
function MyCtrl($scope) {
$scope.statuses = [ 'Draft', 'Pending', 'Live', 'Archived', 'Deleted' ];
$scope.selectedStatuses = [ 'Pending', 'Live' ];
}
.
<select ng-model="selectedStatuses" multiple ng-options="status for status in statuses">
</select>
You're trying to use a select multiple like a checkbox list, which is a little strange. Multi-selects output an array. You can't put ng-model on an option tag like that, it goes on the select itself. So since the select will output an array of values, you'll need to loop through the values and update the nodes in your scope.
Here's a plunk demonstrating the code
And here's the code:
JS
function inArray(x, arr) {
for(var i = 0; i < arr.length; i++) {
if(x === arr[i]) return true;
}
return false;
}
app.controller('MainCtrl', function($scope) {
$scope.query = {
Statuses: {
Draft: true,
Live: true,
Pending: true,
Archived: false,
Deleted: false
}
};
$scope.selectionsChanged = function(){
for(var key in $scope.query.Statuses) {
$scope.query.Statuses[key] = inArray(key, $scope.selectedValues);
}
};
});
HTML
<select multiple ng-model="selectedValues" ng-change="selectionsChanged()">
<option value="Draft" ng-selected="query.Statuses.Draft">Draft</option>
<option value="Pending" ng-selected="query.Statuses.Pending">Pending</option>
<option value="Live" ng-selected="query.Statuses.Live">Live</option>
<option value="Archived" ng-selected="query.Statuses.Archived">Archived</option>
<option value="Deleted" ng-selected="query.Statuses.Deleted">Deleted</option>
</select>
<br/>
{{query | json}}
I hope that helps.
Here is an alternate to blesh solution
app.controller('MainCtrl', function($scope) {
$scope.query = {
Statuses: ["Pending","Live"]
};
});
And select
<select multiple ng:model="query.Statuses" >
<option value="Draft">Draft</option>
<option value="Pending">Pending</option>
<option value="Live">Live</option>
<option value="Archived">Archived</option>
<option value="Deleted">Deleted</option>
</select>
{{query | json}}
Working sample is here:
http://plnkr.co/edit/bCLnOo
Just to point out, IMO multiple select elements are a UI interaction evil. Touch anything without remembering to hold down modifier keys, which some users don't know about, and you lose the whole selection. Especially bad if there enough options that they're not all visible, then you can't even tell when you're dropping an existing selection. Multiple checkboxes are a much better way to represent the same possible options and current selection. A container's worth of them can be made scrollable, effectively similar to a multi-select with more options than size. (Not an answer I know...)