AngularJS - $scope undefined within same controller -


i have controller trying store information in $scope.weather , use it's contents pass function. when log result of $scope.weather[0].latitude when use 1 function when call function within same controller result coming undefined. shouldn't $scope usable within same controller? within same function.

angular.module('cityctrl', []).controller('citycontroller', ['$scope', '$http', 'city', function($scope,  $http,  city){  $scope.update = function (zip) {     city.get({zip : zip}).success(function(response){         $scope.weather = response     }).then(function(response){         $scope.weather = response.data;          // returning expected result         console.log($scope.weather[0].latitude;     })      if(zip.length === 5){          // coming undefined         console.log($scope.weather[0].latitude);          var box = getboundingbox([$scope.weather[0].latitude, $scope.weather[0].longitude], 50);          city.matches(box[1], box[3], box[0], box[2]).success(function(response){             $scope.matches = response         }).then(function(response){             $scope.matches = response.data;             console.log($scope.matches);         })      } } }]); 

this order of operations issue.

when console.log($scope.weather[0].latitude) in if statement $scope.weather has not been set yet because city.get() call asynchronous. means $scope.weather not set until successful response returned city service, in case execute after code block in if statement.

in order if statement code have access $scope.weather need include in .then(function() { // if logic here } portion of code.

also worth noting city.get().success() , city.get().then(successfunction) pretty doing same thing. should use 1 or other, shouldn't need use both.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -