0

I am creating a service to download data from WCF service and then I am binding it to the ionic view. I am not able to bind the data to the view, I keep getting error "profileData is undefined", I have tried to follow the ionic online tutorial.

Inside of config

.state('app.settings', {
                url: '/settings',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/settings.html',
                        controller: 'ProfileCtrl'
                    }
                },
                resolve: {
                    profileData: function(ProfileService){
                        return ProfileService.getUserProfile()
                    }
                }
            })

Profile service

.service('ProfileService', function ($q, $http, $localstorage, loginSharedData, Base64) {
        return {
            getUserProfile: function () {
                console.log('inside service');
                var deferred = $q.defer();
                var promise = deferred.promise;

                var _postData = {
                    "apiKey": Base64.decode($localstorage.get('secureToken')),
                    "userId": Base64.decode(loginSharedData.userid)
                };
                setTimeout(function(){
                $http({
                    method: 'POST',
                    url: 'http://localhost/snc/Service.Svc/GetUserProfile',
                    data: _postData,
                    contentType: 'application/json',
                    dataType: "json"
                }).then(function (response) {
                    console.log('inside service success');
                    var _data = response.data;
                    if (_data.GetUserProfileResult.status) {
                        console.log('Profile downloaded');
                        deferred.resolve(_data.GetUserProfileResult);
                    }
                    else
                        deferred.reject('Failed to get profile.');
                }, function (response) {
                    console.log('inside profile service fail');
                    console.log(response);
                    if (response.status == 0) {
                        deferred.reject('Failed to get profile');
                    }
                })}, 1000);

                promise.success = function (fn) {
                    promise.then(fn);
                    return promise;
                }
                promise.error = function (fn) {
                    promise.then(null, fn);
                    return promise;
                }
                return promise;
            }
        }
    })

The Controller

.controller('ProfileCtrl', function ($scope, ProfileService, Base64) {
        $scope.profileData = profileData;
}

The profileData is undefined in the controller, can you point to the error?

1 Answer 1

1

You have to inject profileData to the controller as well:

.controller('ProfileCtrl', function ($scope, ProfileService, Base64, profileData) {
        $scope.profileData = profileData;
}

btw - you probably don't need to inject ProfileService

Sign up to request clarification or add additional context in comments.

9 Comments

I tried it, but for some reason, I can't go to settings. It changes the URL but nothing comes up on the screen. The button that cause the navigation is in menu.html, bound to AppCtrl and calls "goToSettings()" which does following "$state.go('app.settings')" , it was working earlier.
thanks mate, its working now. Can you point me to some tutorial about sharing data across controllers?
You ninja me before I could finish writing my comment. hehe
@Oro Drori, I can see the data in the console, but I am getting following error when I try to bind "Error: [$injector:unpr] Unknown provider: profileDataProvider <- profileData <- ProfileCtrl"
First - read this article about promises (#4 in particular) - pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.