javascript - Pass variable from config to factory in AngularJS and UI Router -


i'm trying make search in http call different value depending on view i'm in.

my factory looks this:

.factory('searchservice', ['$http','$filter', function($http, $filter) {    var service = {      getallexhibitors : function () {         var searchindex = 'berliner';         var url = '...';         var config = {             params: {                 search: searchindex             },             cache:true         };         $http.get(url, config).then(function (data) {           service.datafairs = data.data.rows;           ...         });     }... 

as can see i'm passing hardcoded variable searchindex parameter search.

could set variable depending on view i'm in?

my config router looks this:

.config(function($stateprovider) {    var berlinerstate = {     name: 'berliner',     url: '/berlinerliste/',     views: {       'header': {            templateurl: 'header.htm'          },       'main':{             templateurl: 'bl2017.htm'           }     }   }    var koelnerstate = {     name: 'koelner',     url: '/koelnerliste/',     views: {       'header': {            templateurl: 'header.htm'          },       'main':{             templateurl: 'kl2017.htm'           }     }   }    ... 

for example, on /berlinerliste, searchindex = x , on /koelnerliste, searchindex = y

any tips?

from controller when call factory, pass actual state name , based on value define searchindex on getallexhibitors method.

getallexhibitors : function (statename) {     var searchindex = '';     var url = '...';     var config = {         params: {             search: searchindex         },         cache:true     };     if(statename === 'berliner'){        config.params.search = 'asdf'     }     ....       $http.get(url, config).then(function (data) {       service.datafairs = data.data.rows;       ...     }); }... 

and controller inject $state service , send method value of $state.current.name

another way inject $state service directly on service, this:

.factory('searchservice', ['$http','$filter','$state', function($http, $filter, $state){        var service = {      getallexhibitors : function () {         var searchindex = 'berliner';         var url = '...';         var config = {             params: {                 search: searchindex             },             cache:true         };         if($state.current.name === 'berliner') {             config.params.search = 'asdf'         }     ....         $http.get(url, config).then(function (data) {           service.datafairs = data.data.rows;       ...     }); }... 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -