I have dropdown in which options are coming from array using ng-repeat. It is working as normal drop-down. But I want in a different way. If one option is selected, it should be disabled in the other dropdowns. can we do like that in angular?
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="address in addresses">
Billing State:
<select
ng-model="address.state"
ng-options="state.lookupCode as state.description for state in lov_state"></select>
<tt>State selected: {{address.state}}</tt>
</div>
</div>
controller code:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.addresses = [
{'state': 'AL'},
{'state': 'CA'},
{'state': 'FL'}
];
$scope.lov_state = [
{'lookupCode': 'AL', 'description': 'Alabama'},
{'lookupCode': 'FL', 'description': 'Florida'},
{'lookupCode': 'CA', 'description': 'California'},
{'lookupCode': 'DE', 'description': 'Delaware'}
];
});
Please help me to do that. Here is the jsfiddle link.
https://jsfiddle.net/santhosh1a32/3vd7hyrq/
You could use disable option provided in ng-options it self by using disable when condition in a place before for keyword.
<div ng-repeat="address in addresses">
Billing State:
<select ng-model="address.state" ng-change="checkSelected(address.state)"
ng-options="state.lookupCode as state.description disable when (address.state != state.lookupCode && state.selected) for state in lov_state">
</select>
<tt>State selected: {{address.state}}</tt>
</div>
Demo Here
Related
Once I use ng-options for a select I am unable to get dafault selection to work on nested json objects.
once I have a bit more complicated json and the select should handle a child object my select does not default select the proper value.
Given test = {"id":3,"title":"Test","product":{"id":4,"name":"Test1"}} as my ng-model test.product and
[{
"id": 4,
"name": "Test1"
}, {
"id": 5,
"name": "Test2"
}]
as my selection option. (see http://embed.plnkr.co/mpnislw77UBSEdHl4UKN/)
I seem to be unable to figure out how to facilitate default selection.
If you use track by item.id it works - http://embed.plnkr.co/mpnislw77UBSEdHl4UKN. The marked answer was not very obious since the ng-model is nested in iself. but it contains the correct information.
The only problem with your code is that you've assigned a new object to $scope.test.product and you're using it as the ng-model of the dropdown.
This makes AngularJS unable to find it inside the possible values, which are $scope.testarray. AngularJS will compare two objects by their reference, which you broke when you assigned a new object to $scope.test.product.
To make it working, change $scope.test as follows:
$scope.test = {
"id": 3,
"title": "Test",
"product": $scope.testarray[1]
}
This is how to select with ngOptions and setting a default value
Example
angular.module('defaultValueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
availableOptions: [
{id: '1', name: 'Option A'},
{id: '2', name: 'Option B'},
{id: '3', name: 'Option C'}
],
selectedOption: {id: '3', name: 'Option C'} //This sets the default value of the select in the ui
};
}]);
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="defaultValueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="mySelect">Make a choice:</label>
<select name="mySelect" id="mySelect"
ng-options="option.name for option in data.availableOptions track by option.id"
ng-model="data.selectedOption"></select>
</form>
<hr>
<tt>option = {{data.selectedOption}}</tt><br/>
</div>
</body>
</html>
try this, hope it will help
<option value="" ng-if="false"></option>
I have dropdown in which options are coming from array using ng-repeat. It is working as normal drop-down. But I want in a different way. If one option is selected, it should be disabled in the other dropdowns. can we do like that in angular? I have some code already in plunker. But the problem is if we select one option and if we change that we are not able to select the previous option. That means, If we are selecting "Florida" in the first dropdown and "california" in second dropdown , then if we change the first drop down value to "Delaware", then we are not able to select all the three options in the dropdown.
Html code is:
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-repeat="address in addresses">
Billing State:
<select
ng-model="address.state"
ng-options="state.lookupCode as state.description for state in lov_state"></select>
<tt>State selected: {{address.state}}</tt>
</div>
</div>
controller code is :
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.addresses = [
{'state': 'AL'},
{'state': 'CA'},
{'state': 'FL'}
];
$scope.lov_state = [
{'lookupCode': 'AL', 'description': 'Alabama'},
{'lookupCode': 'FL', 'description': 'Florida'},
{'lookupCode': 'CA', 'description': 'California'},
{'lookupCode': 'DE', 'description': 'Delaware'}
];
});
Demo Here
So I have a pretty basic select that looks like this:
<ui-select theme="select2" search-enabled="false" ng-model="obj.myModel">
<ui-select-match>{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="item.value as item in choices">
<span>
{{item.name}}
</span>
</ui-select-choices>
</ui-select>
Let's say the choices are defined as
$scope.choices = [
{value: 'some_value_1', name: 'Default name'},
{value: 'some_value_2', name: 'Default name 2'}
]
Now let's assume that the user has selected the item with value some_value_1. After that a server response comes in that updates the choices list with different names
$scope.choices = [
{value: 'some_value_1', name: 'Real name'},
{value: 'some_value_2', name: 'Real name 2'}
]
Notice that the value part that is saved in the model stays the same. The name in <ui-select> is still Default name when I would want it to change to Real name.
plnkr.co
Is there a way to make the selected name correspond to updated choices?
I'm trying to perform an orderBy operation in AngularJS based on a chosen select box value.
The problem I'm having is changing the optional :true or :false flags depending on the selected value.
For example, 'alphabetical' order, the flag would have to be :false, but for 'date' (most recent), the flag needs to be :true.
I've tried hardcoding the flags in the scope options but still not working.
html
<div>
<label>Country filter</label>
<input type="text" ng-model="countryFilter" />
<label>Order by</label>
<select ng-model="selectedOrder" ng-options="option for option in options"></select>
</div>
<ul>
<li ng-repeat="detail in details | filter:{country: countryFilter} | orderBy:selectedOrder">{{ detail }}</li>
</ul>
app.js
var app = angular.module('plunker', []);
app.controller('MyCtrl', function($scope) {
// order by options
$scope.options = ['country', 'address', 'date'];
// all countries
$scope.details = [{
id:1, country:'Finland', address:'Mainstreet 2', date:'2015-05-02'
},{
id:2, country:'Mexico', address:'Some address', date:'2015-05-05'
},{
id:3, country:'Canada', address:'Snowroad 45', date:'2015-03-02'
}];
});
Here is a plunker.
I would suggest to change your order options from strings to objects, which include the information if the list should be reversed for this order:
$scope.options = [{value : 'country', reversed : false}, {value : 'address', reversed : false}, {value : 'date', reversed : true}];
And the ng-repeat to:
<li ng-repeat="detail in details | filter:{country: countryFilter} | orderBy:selectedOrder.value:selectedOrder.reversed">{{ detail }}</li>
http://plnkr.co/edit/JXtHa2jXUy7Y5BsvsdL9?p=preview
I am having the data as below in my controller JS,
Here Subject and exam object is related by the code value in subject object, for example if subject name is "science " then code is "sci" and i try to save this "sci" in mg-model "myOptionSub" and i try to display the data related to "sci" in second dropdown based on model value "myOptionSub" and in div below. I am not able to use the ng-model value which is given by first dropdown in second dropdown.
myApp.controller('parentCtrl', ['$scope', '$window', '$location', function ($scope, $window, $location) {
$scope.subject = {
"subarray": [{
'name': 'science',
'code': 'sci'
}, {
'name': 'maths',
'code': 'mat'
}]
};
$scope.exam = {
"sci": [{
'name': 'science 1',
'examcode': 's1'
}, {
'name': 'science 2',
'examcode': 's2'
}],
"mat": [{
'name': 'maths 1',
'examcode': 'm1'
}, {
'name': 'maths 2',
'examcode': 'm2'
}]
};
}]);
My HTML is
<select
ng-model="myOptionSub"
ng-options="subjectobj.code as subjectobj.name for subjectobj in subject.subarray">
</select>
<div>subject model: {{myOptionSub}}</div>
<div>subject model: {{exam.myOptionSub}}</div>
<select
ng-model="myOptionExam"
ng-options="examobj.examcode as examobj.name for examobj in exam.myOptionSub">
</select>
<div>exam model: {{myOptionExam}}</div>
Here i want to use the selected value of first dropdown (which is stored in model "myOptionSub") as a filter to show the items in selected dropdown.
And My Doubt is in HTML where are the places and how i can use the model values in raw HTML and in any directives like ng-repeat as in my example.
JS Fiddle http://jsfiddle.net/shmdhussain/uh5VU/#run
Is this what you looking for FIDDLE
by using ng-change you could have achieved it
$scope.changesecond = function(){
$scope.exam.myOptionSub = $scope.exam[$scope.myOptionSub];
console.log($scope.examobj)
}