I'm having an issue setting a property in my scope. It does set but only after the second time I click the button that is tied to my controller.
app.controller('ContactsController', ['$scope','$window','$http','googleService', function($scope, $window, $http, googleService) {
$scope.login = function () {
var emails =[];
var promise = googleService.login()
promise.then(function (data) {
console.log(data[0]);
gapi.client.load('gmail', 'v1', function() {
var request = gapi.client.gmail.users.messages.list({'userId' :'me',
labelIds: ['INBOX'],
});
request.execute(function(resp) {
$scope.emails = googleService.makeEmails(resp);
});
});
}
, function (err) {
console.log('Failed: ' + err);
});
};
}]);
And here is my method in my service.
this.makeEmails = function(resp){
var factory = [];
angular.forEach(resp.messages, function(message){
var newEmail = gapi.client.gmail.users.messages.get({'userId': 'me','id': message.id ,'format':'metadata', 'metadataHeaders': ['subject','from','to','date']});
newEmail.execute(function(resp) {
var emailw = resp;
factory.push(emailw);
});
});
return factory;
}
So I click the button that is tied to the click() method in the controller and in the network tab I see all the responses come through and when I step through the code factory is being set. I just can't figure out why the first click scope isn't set but the second click sets it.