I've a quick issue that I can't handle.
I've a data for a dropdown, and depending on the value that's being picked for that dropdown, I want the data to change on the second dropdown, also preferably if it's hidden until the first dropdown is picked.
Atm, I can pick the first controller but I don't have an idea on how to connect the two dropdowns.
Any help will be greatly appreciated. Thank you in advance
new test service
export default class newtest {
constructor($http){
'ngInject';
this._$http = $http;
let testData = this;
this.options = {
releases: [
{value: 1, name: 'R1', environments : ['R1-QA1', 'R1-QA2']},
{value: 2, name: 'R2', environments : ['R2-QA1', 'R2-QA2']},
{value: 3, name: 'R3', environments : ['R3-QA1', 'R3-QA2']}
],
environments : [
['R1-QA1', 'R1-QA2'],
['R2-QA1', 'R2-QA2'],
['R3-QA1', 'R3-QA2']
]
};
}
}
new test controller
class NewTestCtrl {
constructor(NewTest, $state, $http) {
'ngInject';
this._NewTest = NewTest;
this.options = this._NewTest.options;
this.releaseValues = this.options.releases.name;
this.envValues = [];
}
HTML
<fieldset>
<select ng-model="release" ng-options="release.name for release in $ctrl.options.releases">
<option value="" disabled selected>Select</option>
<option value="value">{{name}}</option>
</select>
</fieldset>
<fieldset>
<select ng-model="release" ng-options="env for environments in $ctrl.options.environments">
<option value="" disabled selected>Select</option>
<option value="env">{{env}}</option>
</select>
</fieldset>
First, both your select elements have ng-model="release". This is the data field it is bound to in your controller. The second select element should be something like ng-model="environment".
You should be able to make the environment list change based on the the release selected by having the options use the release data element, something like ng-options="env for environments in $ctrl.options.releases[release].environments"
Lastly, you could hide the environment select until a release is selected with a ng-Show attribute based on release having a value. Something like ng-Show="release>0" and initialize release to 0 in your constructor.
Thanks for the help #Jim Moore, I found a good fiddle link that I followed for guidelines https://jsfiddle.net/annavester/Zd6uX/
And I changed the json of my data to >
environments : {
'Release 1' : ['R1-QA1', 'R1-QA2'],
'Release 2' : ['R2-QA1', 'R2-QA2'],
'Release 3' : ['R3-QA1', 'R3-QA2']
}
and also the html to, For anyone going through the same issue in the future, I hope this helps.
<fieldset>
<select id="release"
ng-model="$ctrl.release"
ng-hide="!$ctrl.browsers"
ng-options="release for (release, environments) in $ctrl.environments">
<option value="" disabled selected>Select</option>
</select>
</fieldset>
<fieldset>
<select id="environment"
ng-model="$ctrl.environment"
ng-hide="!$ctrl.release"
ng-options="environment for (release, environment) in $ctrl.release">
<option value="" disabled selected>Select</option>
</select>
</fieldset>
Related
I have list of locations in a select box. On selection of a location, the corresponding incidenttypes should be displayed in another select box.
$scope.locationNames=[
{id:"Onboard",value:"On-board service"},
{id:"Clubhouse",value:"Clubhouse"},
{id:"Gate",value:"Gate"}
];
$scope.incidentTypesList={
Onboard:[
{id:"IFE faulty",value:"IFE faulty"},
{id:"223",value:"No special meal as ordered"},
{id:"Spoilt",value:"Spoilt/damaged belongings"}
];
Clubhouse:[
{id:"",value"No appointments available"},
{id:"",value="Late/delayed transport service"},
{id:"",value="Facilities not available"}
];
};
I am able to get location names in the list using the below code.
<select class="firstDropDown" ng-model="location" ng-options="item.id as item.value for item in locationNames">
<option value="">Select location</option>
</select>
Can you please help me how to implement on selection on this to show the values in another select box.
I think the solution below fits your needs. There were some syntax errors in your array which needed to be fixed.
HTML
<div ng-app="myApp" ng-controller="myAppCtrl">
<select class="firstDropDown" ng-model="location" ng-options="item.id as item.value for item in locationNames">
<option value="">Select location</option>
</select>
<select class="secondDropDown" ng-model="incident" ng-options="incident.id as incident.value for incident in incidentTypesList[location]">
<option value="">Select incident</option>
</select>
</div>
JS
var myApp = angular.module("myApp", [])
myApp.controller("myAppCtrl", function($scope){
$scope.locationNames=[
{id:"Onboard",value:"On-board service"},
{id:"Clubhouse",value:"Clubhouse"},
{id:"Gate",value:"Gate"}
];
$scope.incidentTypesList={
Onboard:[
{id:"IFE faulty",value:"IFE faulty"},
{id:"223",value:"No special meal as ordered"},
{id:"Spoilt",value:"Spoilt/damaged belongings"}
],
Clubhouse:[
{id:"",value:"No appointments available"},
{id:"",value:"Late/delayed transport service"},
{id:"",value:"Facilities not available"}
]
}
});
JSFiddle: https://jsfiddle.net/ABr/wqy2ha9o/3/
Yesterday angular 1.4.0 was released.
The changelog states that there is a breaking change with "selects".
I used to use selects like this:
controller code:
// Default initial selection
$scope.filters = {
someFilter: false
};
view code:
<div class="form-group">
<label>Filter...</label>
<select class="form-control input-sm" ng-model="filters.someFilter">
<option value=""></option>
<option value="true">is true</option>
<option value="false">is false</option>
</select>
</div>
And the select would start with the option "is false" selected.
Now, with angular 1.4.0, that is not the case:
The selected option is "blank". I see two blanks in the rendered view (instead of one, as I did before)
How do I fix this?
As noted in the docs...
The value of a select directive used without ngOptions is always a string.
Essentially, you are trying to compare the string "false" with the boolean false.
One possible solution is to use ngOptions, eg
$scope.options = [
{ val: true, label: 'is true' },
{ val: false, label: 'is false' }
];
and in your template
<select ng-model="filters.someFilter"
ng-options="opt.val as opt.label for opt in options">
<option value=""></option>
</select>
Plunker
Alternatively, you could just use strings in your model, eg
$scope.filters = {
someFilter: "false"
};
Plunker
I'm almost there but stuck on one little detail. I can't seem to get the "name" property of the selected Car Make's "Car Model" to actually show up on the select menu. At the moment, it seems to be showing the correct number of options, but the options are visibly BLANK. Here's my code to help better explain...
$scope.carMakes = [
{
name: 'Honda',
models: [{name:'Accord'}, {name: 'Civic'}, {name: 'CRX'}]
},
{
name: 'Toyota',
models: [{name:'Camry'}, {name: 'Forerunner'}]
}
];
<div class="input-group">
<label>Vehicle Make</label>
<select id="carMake" ng-model="carMake" ng-options="carMake as carMake.name for carMake in carMakes track by carMake.name">
<option value="">All</option>
</select>
</div>
<div class="input-group carModels">
<label>Vehicle Model</label>
<select id="carModel" ng-model="carMake.model" ng-options="carMake.model as carMake.model.name for carMake in carMake.models">
<option value="">All</option>
</select>
</div>
Can someone please tell me why even though the carModel select options show up, the "name" isn't being displayed? Thank you!
You want something like this:
<select id="carModel" ng-model="carMake.model" ng-options="carModel as carModel.name for carModel in carMake.models">
<option value="">All</option>
</select>
We're using carModel.name as the display and setting the value as the object itself. So in your controller, $scope.carMake.model will be equal to the model object. So if the user selects "Accord" then in your controller:
$scope.carMake.model.name === 'Accord' // True
would evaluate to true after the selection is made.
I have this select dropdown with "Enbridge Billing" selected.
<select class="form-control required ng-pristine ng-valid" ng-model="finance.payment_method" id="payment_method" name="payment_method">
<option value=""></option>
<option value="EGD" selected="selected">Enbridge Billing</option>
<option value="PAPP">PAD Billing</option>
</select>
However, on screen, it's not selected:
I've removed all the stuff from app.js and still the problem presists:
var app = angular.module('portal', []);
app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('%%');
$interpolateProvider.endSymbol('%%');
});
app.controller('NewJobFinanceController',
function ($scope) {
});
Edit:
I just did this in the controller:
$scope.finance.payment_method = $('#payment_method').find(':selected').val();
The single point of truth in angular is the model. Not the view. You update the model, and the view updates itself to reflect the state of the model.
You configured the select box as
<select ng-model="finance.payment_method" ...>
So that means that the currently selected value is finance.payment_method. That's where angular gets the selected value. Since it's undefined, you have a blank select box.
BTW, you should not set, required, ng-pristine and ng-valid as class of your select box. angular will add and remove this classes by itself, depending on the actual state of the form control.
Simply use ng-selected="true" instead of selected="selected".
I am trying to populate the currently selected item in angular js.
The currently selected item is that with the id in event.email.chases[0].id
and I am trying to match it to a select box populated from case.activeChases
This does what I want so far, which updates whenever the select box changes.
<select
class="form-control"
name="chase"
id="chase"
ng-model="customer.chase"
ng-change="addEmailToChase(customer.chase.id, event.email.id)"
ng-options="cor as cor.emails[0].subject for cor in case.activeChases">
<option value="">None</option>
</select>
I need to find the chase in activeChases with activeChases[x].id = event.email.chases[0].id, so the correct value is selected upon the page load.
Could I possibly do this all within a ng-selected attribute?
Something wrong with your model and your ng-options, the object should be the same if you want an "auto-selected"
without a jsfiddle it's hard to reproduce your situation but I tryed something like that
var app = angular.module('App', []);
app.controller('Ctrl', function($scope) {
$scope.case = {
activeChases: [{
emails: [{
subject: 'toto'
}]
}, {
emails: [{
subject: 'tata'
}]
}]
};
// set "default value"
$scope.customer = {
chase: $scope.case.activeChases[1]
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="App" ng-controller="Ctrl">
<select class="form-control" name="chase" id="chase" ng-model="customer.chase" ng-change="addEmailToChase(customer.chase.id, event.email.id)" ng-options="cor as cor.emails[0].subject for cor in case.activeChases">
<option value="">None</option>
</select>
</section>
So this was my answer:
<select
class="form-control"
name="chase"
id="chase"
ng-model="event.email.chases[0].id"
ng-change="addEmailToChase(event.email.chases[0].id, event.email.id)"
ng-options="cor.id[0] as cor.emails[0].subject for cor in case.activeChases">
</select>
I wanted the default value to be event.email.chases[0].id in the select, so I just set ng-model to that, and changed the first option to cor.id[0] to use id in index.
I chose a random name for ng-model which was customer.chase which made no sense. event represents an ajax loaded form element so its safe for me to do this in my scope.
Also worth noting that the id in cor.id[0] is actually in a size one array due to incorrect design in the app.