This is the solution I found - I welcome alternative/better ways to do this.
Apparently $scope.weather
references multiple values. That is to say, because the $resource and $http methods return promises and the nature of those promises, $scope.weather
can actually reference two separate objects as far as the view and controller are concerned. The way I solved the problem was by using $rootScope
to make sure the same weather
object was always overwritten.
Here's the new code:
'use strict';
angular.module('myApp')
.controller('WeatherCtrl', function ($scope, Restangular, $rootScope) {
// Get initial weather data (NOW WITH $rootScope)
Restangular.one('current').get().then(function(weather) {
$rootScope.weather = weather.weather;
$scope.geolocation = weather.location;
});
// Search functionality
$scope.weatherLookup = function(query) {
Restangular.one('location/' + query).get().then(function(newWeather) {
$rootScope.weather = newWeather;
console.log($rootScope.weather);
});
console.log($rootScope.weather);
};
});
I switched from using Angular's own $resource
and $http
services to the wonderful Restangular library. Despite this change, the original problem persisted until I used $rootScope
. I tested this theory using $resource
and $http
and it still worked so I know that the issue was that $scope.weather
was somehow splitting off and referencing two separate objects under the hood because of the way $scope
and promises work in Angular.