简体   繁体   中英

AngularJS pass variable into factory

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 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM