I've got a AngularJS Material Select:
<md-input-container style="margin-right: 10px;">
<label>Roll</label>
<md-select id="acc-type" required="" ng-model="rolle">
<md-option ng-selected="true" value="Tenant">Tenant</md-option>
<md-option value="Lessor">Lessor</md-option>
</md-select>
</md-input-container>
and I want to get the actual selected option. I tried it with
var e = document.getElementById("acc-type");
var selectedItem = e.value;
but all I receive is undefined. Is there any option to get the value of a md-select in AngularJS?
Since you have already defined ng-model="rolle" in your md-select, you can access it via $scope.rolle
Try:
var selectedItem = $scope.rolle
Its in angular Js and model is already assigned so try
Try
console.log($scope.rolle);
I'm trying to check for changes in the md-select, but I'm not able to achieve it with $dirty
<div class="resourceForm" ng-form="resForm{{$index}}">
<md-select name="resourceAllocForm" id="id{{resourceContent.activityId}}-{{x}}" ng-model="selRes[$parent.$index][x]" placeholder="Select Resource" class="md-no-underline" ng-keyup="resourceCheck($parent.$index)">
<md-option ng-value="resource.id" ng-repeat="resource in resourceList track by resource.id">{{resource.roleProLabel}}</md-option>
</md-select>
</div>
and I have tried something like this
$timeout(function(parentIndex){
$scope.resourceAllocForm['resForm' + parentIndex].$dirty;
console.log('Form Edited');
}
You need something like this,
<md-select ng-change="show(resource)" ng-model="selectedresource">
<md-option ng-repeat="resource in resourceList track by resource.id" value="{{resource.id"}}">{{resource.roleProLabel}}
</md-option>
<md-select>
and in controller
$scope.show = function(resource){
console.log(show);
}
I am using Angular Material to make my new web app, I want to get the selected value from an <md-select> and the problem is that I am new to angularjs and how it works. If anyone could help me, it would be really appreciated.
HTML :
<md-input-container>
<label>Rating</label>
<md-select ng-model="userRating">
<md-option><em>None</em>
</md-option>
<md-option ng-repeat="rate in ratings" ng-value="rate.abbrev">
{{rate.abbrev}}
</md-option>
</md-select>
</md-input-container>
You can just get the value by using the $scope variable
In controller
console.log($scope.userRating);
Inorder to get the selected value on change , you can pass the selected to a function and display,
<md-select ng-change="Print()" class="inline-flex md-select-down" placeholder="Select" ng-model="model">
<md-option ng-repeat="item in ratings" value="{{item.value}}">
{{item.abbrev}}
</md-option>
</md-select>
Controller:
$scope.Print = function(){
alert($scope.model);
}
DEMO
I have a hostSettings object in which there is a host object. "cohostLists" is an array of host object. I'm getting the "cohostLists" as an array of objects and the selected cohost are also saved but not being set in page.
model
#ManyToMany(cascade = CascadeType.PERSIST)
#JoinTable(name = "host_settings_cohost_list",
joinColumns = #JoinColumn(name="host_settings_id", referencedColumnName="ID"),
inverseJoinColumns = #JoinColumn(name="cohost_lists_id", referencedColumnName="ID"))
private Set<Host> cohostLists = new HashSet<>();
html
<md-input-container flex="55">
<md-select ng-model="vm.hostSettings.cohostLists" multiple="true" ng-click="vm.getCoHostList()">
<md-optgroup >
<md-option ng-value="host" ng-repeat="host in vm.coHostList">{{host.user.firstName}}</md-option>
</md-optgroup>
</md-select>
</md-input-container>
cohostLists :
cohostLists:Array[1]
0:Object
$$mdSelectId:1
deleted:false
department:null
designation:"director"
dob:null
id:2
mobileNumber:"98765"
officePhoneNumber:"34355"
organisation:null
profilePic:"dir.jpg"
user:Object
activated:true
deleted:null
email:"host#gh.fgbd"
firstName:"host1"
id:5
langKey:"en"
lastName:"hh"
login:"host"
organisation:null
resetDate:null
resetKey:null
I think the problem is in md-select code.But I couldnt find the bug
Finally I got the answer
Change the html code like this:
<md-select ng-model="vm.hostSettings.cohostLists"
ng-model-options="{trackBy: '$value.id'}" multiple="true">
<md-option ng-value="host" ng-repeat="host in vm.coHostList">{{
host.user.firstName}}
</md-option>
</md-select>
I have a md-select set up as follows:
<md-select placeholder="Category" ng-model="current.Category" flex >
<md-option ng-repeat="item in categories" ng-value="{{item}}">{{item.Name}}</md-option>
</md-select>
#scope.categories value is
[
{
"Name":"Commercial & Industrial",
"ParentId":null,
"Categories":[
{
"Name":"Deceptive Marketing",
"ParentId":19,
"Categories":[
],
"Id":24,
"ModifiedDate":"2015-08-06T07:49:53.0489545",
"CreatedDate":"2015-08-06T15:49:51.707"
},
{
"Name":"Aggressive Agents",
"ParentId":19,
"Categories":[
],
"Id":25,
"ModifiedDate":"2015-08-06T07:50:10.0026497",
"CreatedDate":"2015-08-06T15:50:08.63"
}
],
"Id":19,
"ModifiedDate":"08/06/2015 # 7:49AM",
"CreatedDate":"08/06/2015 # 3:49PM"
},
{
"Name":"Competitive Supply",
"ParentId":null,
"Categories":[
{
"Name":"Security Deposit",
"ParentId":20,
"Categories":[
],
"Id":21,
"ModifiedDate":"2015-08-06T07:49:30.3966895",
"CreatedDate":"2015-08-06T15:49:25.8"
},
{
"Name":"Meter",
"ParentId":20,
"Categories":[
],
"Id":22,
"ModifiedDate":"2015-08-06T07:49:34.6571155",
"CreatedDate":"2015-08-06T15:49:33.3"
},
{
"Name":"Bill",
"ParentId":20,
"Categories":[
],
"Id":23,
"ModifiedDate":"2015-08-06T07:49:41.7268224",
"CreatedDate":"2015-08-06T15:49:40.357"
}
],
"Id":20,
"ModifiedDate":"08/06/2015 # 7:49AM",
"CreatedDate":"08/06/2015 # 3:49PM"
}
]
The md-select works fine. But what I can't figure out is how to set select value. When I try setting the model current.Category to one of the values from the $scope.categories it doesn't get set.
The documentation isn't explicit, but you should use ng-selected. I've created a codepen to illustrate, but basically:
<md-option ng-repeat="(index,item) in categories" ng-value="{{item}}"
ng-selected="index == 1">
{{item.Name}}
</md-option>
This'll select the the second category (index 1 in your category array).
You need to use ng-model-options, trackBy and choose a unique model field as a selector:
<md-select placeholder="Category"
ng-model="current.Category"
ng-model-options="{trackBy: '$value.Id' }" // <-- unique field 'Id'
flex >
<md-option
ng-repeat="item in categories"
ng-value="{{item}}">{{item.Name}}</md-option>
</md-select>
This solution is described in Angular Material's official documentation.
in order to set the default value of select you can use ng-selected and ng-model
<md-select ng-model="vmIdPage.number">
<md-option ng-value="mod" ng-repeat="mod in vmIdPage.initObject.listeModaliteMisePlace" ng-selected="{{ mod.id === vmIdPage.number.id ? 'true' : 'false' }}">
{{mod.libelle}}
</md-option>
</md-select>
This solution is simple if you are wanting to default to the first value in the drop down.
Use the ng-select="$first". This will default the drop down to the first value.
<md-select placeholder="Categories" ng-model="current.category">
<md-option ng-repeat="(index, item) in categories" value="{{item}}"
ng-selected="$first">{{item.Name}}</md-option>
</md-select>
Here is a CodePen to demonstrate.
In my case adding ng-model-options="{trackBy: '$value.id'}" as described in the docs did not work, as no initial value was set. By explicitly setting the model to the wished default value in the controller properly set the value as desired. This approach might be easier if you do not know up front the index of the element you want to display as pre-selected (using ng-selected). Of course you can evaluate it in the controller, but to me it seems more immediate to set the target element directly to the model.
View:
<div class="input-style-border">
<md-select ng-model="vm.selectedGlobalFilter">
<md-option ng-value="item" ng-repeat="item in vm.globalFilterValues">
{{item.value}}
</md-option>
</md-select>
</div>
Controller:
function initialize() {
vm.globalFilterValues = globalFilterValues;
vm.selectedGlobalFilter = TransferGlobalFilter.Worldwide;
}
Where globalFilterValues are in the form:
[
{key:"All", value:"All values" },
{key:"Manager", value:"Manager"},
{key:"HR", value:"Human resources"},
]
Use value in ng-option insted of ng-value
Like specified in the doc of md-select directive , you can use ng-model-options.
See this post