In my app I have a controller that takes a user input consisting of a twitter handle from one view and passes it along through my controller into my factory where it is sent through to my backend code where some API calls are made and ultimately I want to get an image url returned.
I am able to get the image url back from my server but I have been having a whale of a time trying to append it another view. I've tried messing around with $scope and other different suggestions I've found on here but I still can't get it to work.
I've tried doing different things to try and get the pic to be interpolated into the html view. I've tried injecting $scope and playing around with that and still no dice. I'm trying not to use $scope because from what I understand it is better to not abuse $scope and use instead this
and a controller alias (controllerAs
) in the view.
I can verify that the promise returned in my controller is the imageUrl that I want to display in my view but I don't know where I'm going wrong.
My controller code:
app.controller('TwitterInputController', ['twitterServices', function (twitterServices) {
this.twitterHandle = null;
// when user submits twitter handle getCandidateMatcPic calls factory function
this.getCandidateMatchPic = function () {
twitterServices.getMatchWithTwitterHandle(this.twitterHandle)
.then(function (pic) {
// this.pic = pic
// console.log(this.pic)
console.log('AHA heres the picUrl:', pic);
return this.pic;
});
};
}]);
Here is my factory code:
app.factory('twitterServices', function ($http) {
var getMatchWithTwitterHandle = function (twitterHandle) {
return $http({
method: 'GET',
url: '/api/twitter/' + twitterHandle
})
.then(function (response) {
return response.data.imageUrl;
});
};
return {
getMatchWithTwitterHandle: getMatchWithTwitterHandle,
};
});
this is my app.js
var app = angular.module('druthers', ['ui.router']);
app.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('/', {
url: '/',
templateUrl: 'index.html',
controller: 'IndexController',
controllerAs: 'index',
views: {
'twitter-input': {
templateUrl: 'app/views/twitter-input.html',
controller: 'TwitterInputController',
controllerAs: 'twitterCtrl'
},
'candidate-pic': {
templateUrl: 'app/views/candidate-pic.html',
controller: 'TwitterInputController',
controllerAs: 'twitterCtrl'
}
}
});
});
Here is the view where I want to append the returned image url
<div class="col-md-8">
<img class="featurette-image img-circle" ng-src="{{twitterCtrl.pic}}"/>
</div>