10

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;

});
3
  • 1
    possible duplicate of Pass variable to factory angularjs Commented May 18, 2015 at 18:28
  • Where is selectCity coming from? Don't you need to put that as an argument? Commented May 18, 2015 at 18:30
  • @YuujinLee Its from <select ng-model="selectCity"></select> Commented May 18, 2015 at 18:34

4 Answers 4

11

I 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

Sign up to request clarification or add additional context in comments.

6 Comments

May i ask another question ? How can i pass $routeParams too as a argument in to factory ?
Take $routeParams values like respective controllers show here thinkster.io/a-better-way-to-learn-angularjs/routeparams-api and pass like APIservices.getAPIcarModels($routeParams.routeParamsName);
Now not working existing argument when i implement your code , I have to pass two argument or I need find another way instead of first argument - Any idea ?
Ok forget $routeParams I dont need confuse you . Just think I have another select tag too same as we have already done jsfiddle.net/VSph2/593
|
2

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')
}

Comments

1

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

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.