javascript - Angular services issue -


i need understand wrong in following angular service code.

edit: have few controllers in different js files use crud api like

$http.get('/api/sites/' + service.siteid) 

or need variables directive like

templateurl: '../../' + service.template + ' + '.html' 

so create service share variables between js file instead of making api call in each file.

here service

app.service('shareddata', function($http) {  $http.get('config.json')      .success (function(data) {           var siteid = data._id;         console.log(siteid); // return 553e2d15f91e4bd75d000008           $http.get('/api/sites/' + siteid)             .success(function(site) {                 var template = site.template;                 console.log(template); // return mytemplate             })             .error(function(data) {                 console.log('error: ' + data);             })     })     .error(function(data) {         console.log('error: ' + data);     })  service = {     siteid :    siteid, // return undefined instead of 553e2d15f91e4bd75d000008     template :  template // return undefined instead of mytemplate                           };  return service; }) 

i want make precision. if write service below, works fine, mean value passed in controllers , directives

app.service('shareddata', function() { service = {     siteid : '553e2d15f91e4bd75d000008',     pageid : '553e2d15f91e4bd75d000009',     template : 'template1',     layout : 'home'     };  return service; }) 

many thanks

edit

i have change service follow

app.factory('shareddata', function($http) { service = {}; $http.get('config.json')      .success (function(data) {           service.siteid = data._id;          //console.log(service.siteid);          $http.get('/api/sites/' + service.siteid)             .success(function(site) {                 service.template = site.template;                 //console.log(service.template);             })     }) service.pageid = '553e2d15f91e4bd75d000009'; service.layout = 'home' return service }) 

in controller console.log(service) return object value :

layout: "home" pageid: "553e2d15f91e4bd75d000009" siteid: "553e2d15f91e4bd75d000008" template: "template1"  console.log('layout ' + service.layout); // return home console.log('pageid ' + service.pageid); // return 553e2d15f91e4bd75d000009 

but

console.log('siteid ' + service.siteid); // return undefined  !!!!! console.log('template ' + service.template); // return undefined !!!!! 

do not understand please help

your siteid variable referenced outside of $http.get method's scope.

define siteid above function so:

app.service('shareddata', function($http) { var siteid = ''; $http.get('config.json')      .success (function(data) {           siteid = data._id;         console.log(siteid); // return 553e2d15f91e4bd75d000008           $http.get('/api/sites/' + siteid)             .success(function(site) {                 var template = site.template;                 console.log(template); // return mytemplate             })             .error(function(data) {                 console.log('error: ' + data);             })     })     .error(function(data) {         console.log('error: ' + data);     })  service = {     siteid :    siteid, // return undefined     template :  template,                            };  return service; }) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -