javascript - how to properly handle variable scope in AngularJS -
in anlgularjs controller, first order of business determine if app running in dev, or test.
for purpoer, make call api, an answer (that part verified work).
depending on environment, need set value $scope.uploadurl, file uploader directive know url target file uploads.
for reason, time uploader executed, url value lost.
here relevant part of controller:
$scope.uploadurl = ''; console.log('this fileuploadcontroller'); function getenvironment (){ $http.get('/environment').success(function(response) { $scope.currentenvironment = response.environment; if(response.environment === 'test'){ $scope.uploadurl = 'http://test_url:3001/api/files'; } if(response.environment === 'dev'){ $scope.uploadurl = 'http://dev_url:3000/api/files'; } console.log('current environment is: ' + $scope.currentenvironment + ' uploadurl should be: ' + $scope.uploadurl); }); } getenvironment(); var selectedcategory; var selecteddataversion; var uploader = $scope.uploader = new fileuploader({ //url: 'http://dctool-lnx.cloudapp.net:3001/api/files', url: $scope.uploadurl, tabname: 'sheet1' });
what proper way re-structure code, make value, set within getenvironment function live long enough?
for reason, time uploader executed, url value lost.
not really, actual reason time uploaded executed, url value has not been assigned yet because operation assigns value async (asycjax) in nature.
so need wait operation complete, can utilizing promise returned operation.
//return promise function function getenvironment (){ return $http.get('/environment').... } //make call chain through , register runuploader getenvironment().then(runuploader); var selectedcategory; var selecteddataversion; function runuploader(){ var uploader = $scope.uploader = new fileuploader({ //url: 'http://dctool-lnx.cloudapp.net:3001/api/files', url: $scope.uploadurl, tabname: 'sheet1' }); }
Comments
Post a Comment