Shikha thakur
Shikha thakur

Reputation: 1289

$state.go doesn't seems to be working

I have a scenario where when click on searched item i need to load different state

Here is my html code

<span class="item-title" ng-click="fnViewProfile()">
      <span> {{item.name}} </span>
</span>

below is my controller code:

$scope.fnViewProfile = function() {
        $state.go("ViewProfile", {
            "id": 10215
        });
    }

In my app.js i have a resolve function where i did ajax call to get the data before the html gets loaded

.state('ViewProfile', {
            parent: 'home',
            url: '/ViewProfile:id',
            templateUrl: 'views/ViewProfileView.html',
            controller: 'ProfileViewCtrl',
            resolve: {
                profile: function(DashboardService, $stateParams, $scope) {
                    debugger;
                    var userId = $stateParams.id;
                    var userData;
DashboardService.fnGetUser(userId).then(function(oResponse) {
                        userData = oResponse.data;

                    })
return userData;
                }
            }

In controller of ViewProfile state i am passing the profile service

angular.module('talentGraphApp')
.controller('ProfileViewCtrl', function($scope, $state, DashboardService, profile) {
    debugger;
    $scope.profile = profile;
    console.log(profile);
});

But i am unable to get profile in the console.

I dont understand where i am going wrong

Any help would be appreciated.

Upvotes: 0

Views: 68

Answers (1)

charlietfl
charlietfl

Reputation: 171679

In the resolve you return userData before it has been assigned inside the promise callback, so it is undefined when you pass it to controller

Return the promise instead

Change to

 resolve: {
      profile: function(DashboardService, $stateParams) {                      
             var userId = $stateParams.id;            
             return DashboardService.fnGetUser(userId).then(function(oResponse) {
                        // return as resolved value
                        return oResponse.data;    
             });    
        }
}

Also there is no $scope in the routing config

Upvotes: 1

Related Questions