I have the following select
list:
<select name="make" class="form-control" ng-model="selectCity">
<option value="Kannur">Kannur</option>
<option value="Agra">Agra</option>
<option value="Ahemedabad">Ahemedabad</option>
<option value="Bangalore">Bangalore</option>
<option value="Chennai">Chennai</option>
<option value="Mumbai">Mumbai</option>
</select>
When I change the selected option I need to pass the ng-model
, selectCity
into a factory which calls an API
:
The factory:
carPriceApp.factory('APIservices', function($http){
APIcarModels = {};
APIcarModels.getAPIcarModels = function(){
return $http.get('/carprices3/api/apiData'+ selectCity +'.js')
}
return APIcarModels;
});
I have the following select
list:
<select name="make" class="form-control" ng-model="selectCity">
<option value="Kannur">Kannur</option>
<option value="Agra">Agra</option>
<option value="Ahemedabad">Ahemedabad</option>
<option value="Bangalore">Bangalore</option>
<option value="Chennai">Chennai</option>
<option value="Mumbai">Mumbai</option>
</select>
When I change the selected option I need to pass the ng-model
, selectCity
into a factory which calls an API
:
The factory:
carPriceApp.factory('APIservices', function($http){
APIcarModels = {};
APIcarModels.getAPIcarModels = function(){
return $http.get('/carprices3/api/apiData'+ selectCity +'.js')
}
return APIcarModels;
});
Share
Improve this question
edited May 18, 2015 at 19:00
lin
18.4k4 gold badges65 silver badges87 bronze badges
asked May 18, 2015 at 18:24
HunterHunter
1,6014 gold badges16 silver badges27 bronze badges
3
- 1 possible duplicate of Pass variable to factory angularjs – Robin Vinzenz Commented May 18, 2015 at 18:28
- Where is selectCity coming from? Don't you need to put that as an argument? – Eugene J. Lee Commented May 18, 2015 at 18:30
- @YuujinLee Its from <select ng-model="selectCity"></select> – Hunter Commented May 18, 2015 at 18:34
4 Answers
Reset to default 11I solved your problem write html here
<div ng-controller="myCtrl">
<select ng-change="changeCity()" name="make" class="form-control" ng-model="selectCity">
<option value="Kannur">Kannur</option>
<option value="Agra">Agra</option>
<option value="Ahemedabad">Ahemedabad</option>
<option value="Bangalore">Bangalore</option>
<option value="Chennai">Chennai</option>
<option value="Mumbai">Mumbai</option>
</select>
</div>
and javascript here
var app = angular.module('myApp', []);
app.factory('APIservices', function($http) {
var APIcarModels = {};
APIcarModels.getAPIcarModels = function(selectCity) {
alert(selectCity);
return $http.get('/carprices3/api/apiData'+ selectCity +'.js')
}
return APIcarModels;
});
function myCtrl($scope, APIservices) {
$scope.changeCity = function() {
APIservices.getAPIcarModels($scope.selectCity);
};
}
Working example Here
You can use the ng-change
property on your select list and from there call your API. You'll also need to take a parameter in your getAPIcarModels
function:
Your markup would be like:
<select name="make" class="form-control" ng-model="selectCity" ng-change="selectMake()">
And in your controller:
$scope.selectMake = selectMake() {
APIservices.getAPIcarModels($scope.selectCity);
}
And finally, your factory:
APIcarModels.getAPIcarModels = function(selectCity){
return $http.get('/carprices3/api/apiData'+ selectCity +'.js')
}
you need to add something like ng-change="changeCity(selectCity)"
Refer below articles for example and more information
http://plnkr.co/edit/0IVNLHiw3jpz4zMKcB0P?p=preview
http://stackoverflow.com/questions/26485346/how-do-i-get-the-ng-model-of-a-select-tag-to-get-the-initially-selected-option
http://stackoverflow.com/questions/25935869/id-as-ng-model-for-select-in-angularjs
alternatively: using ng-options
:
view:
<div ng-app='App' ng-controller="AppCtrl">
<select ng-model="selectedCity" ng-options="city.name for city in cities">
</select>
Selected City is {{selectedCity.name}}
</div>
js:
angular.module('App', [])
.controller('AppCtrl', function($scope) {
$scope.cities = [
{name: 'Chicago'},
{name: 'London'}
];
$scope.selectedCity = '';
});
working example: https://jsfiddle.net/pz9f093e/