离子angularJS,获取数据并绑定到视图

时间:2015-08-31 19:09:27

标签: ajax angularjs wcf ionic-framework

我正在创建一个从WCF服务下载数据的服务,然后我将它绑定到离子视图。我无法将数据绑定到视图,我不断收到错误" profileData未定义",我试图按照离子在线教程。

配置内容

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

个人资料服务

.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;
            }
        }
    })

控制器

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

控制器中的profileData未定义,你能指出错误吗?

1 个答案:

答案 0 :(得分:1)

您还必须将profileData注入控制器:

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

btw - 您可能不需要注入ProfileService