Sam5487 Sam5487 - 1 year ago 131
AngularJS Question

AngularJS $scope variable will not display in ionic view using GET request

I am having an issue displaying response data, returned from my factory,inside an ionic view page. I believe the issue has something to do with the way I am handling the promise, but I cannot pinpoint why. So to start the roadmap to my problem, here is my:


.factory('profileFactory', function ($http, $q) {
var config = {
headers: {
'MyKey': 'myvalue'
this.getEmployee = function (userId) {
return $http.get('http://someurlpathtogoto' + userId + 'etc', config).then(function (response) {
return this;

The above code returns the JSON object I need for my controller so:


.controller('EmployeeCtrl', ['$scope', 'profileFactory', function ($scope, profileFactory) {

//Set back to false
$scope.profileLoaded = true;
$scope.serviceUnavailable = false;

$scope.setCommitId = function (userId) {

$scope.employee = arrItems;
$scope.firstName = $scope.employee.record[0].Preferred_First_Name;
$scope.lastName = $scope.employee.record[0].Preferred_Last_Name;

Now when my page displays I want it to look like such:

Ionic View (Profile)

<ion-view view-title="Profile" ng-controller="EmployeeCtrl" hide-nav-bar="true">
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
<div ng-show="serviceUnavailable">
Directory service unavailable
<div ng-show="profileLoaded">
<br />
<center><img class="focus-img-circle" ng-src="default.png" /></center>
<center><b>{{firstName}} {{lastName}}</b></center>

All of this is invoked whenever a user presses on an arrow button in the app which should take them to their profile page to view some info. The following code appears in the previous view.

Ionic View (Search Results)

<ion-item class="item item-avatar item-button-right" ng-repeat="user in results" ng-controller="EmployeeCtrl">
<img ng-src="data:image/png;base64,{{user.pic}}">
<strong>{{user.first_name}} {{user.last_name}}</strong>
<div class="buttons" style="padding:20px;">
<button type="button" class="button button-icon ion-ios-telephone-outline customSearchIcon" ng-click=""></button>
<a class="button button-icon ion-ios-arrow-right customSearchIcon" ng-click="setCommitId(" href="#/tab/search/profile"></a>
<button type="button" class="button button-icon ion-ios-heart-outline customSearchIcon" ng-click="addFavorite(, user.first_name, user.last_name, user.pic)"></button>

So essentially a user clicks on a persons tab, takes them to that profile where my factory is used to make a RESTful call to return a JSON object containing data about that user. Except nothing is being displayed in the Ionic View. If I hard code a userId in the controller and take away the function setCommitId() it works but that obviously isn't an option. Does anyone see anything specifically that I am doing wrong? Been stuck on this for hours any help would be great.

I left a lot of code out to get my point across.


The two views you see are different ionic templates. So using ng-controller="EmployeeCtrl is not happening twice in the same view.

Answer Source

The mistake is, on arrow click, you call the service and store the data to scope variable and then navigates to second view. The second view though uses the same controller as the first view, a new controller is being instantiated with empty scope - and hence your view is empty.

Instead of ng-click in <a> tag, modify your profile route to pass the userid as well - tab/search/profile/{userid} and in anchor tag have ng-href= "#/tab/search/profile/{{})" and in your profile controller get the userid from query string ($ and make the Ajax call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download