0

I'm trying get data from db to UI. Url given via provider is getting the data.

Controller in controller DetailsProvider.getDashboardDetails() is getting null.

var appmod = angular.module('project.DetailDashboardController', []);

appmod.controller("DetailDashboardController", ['$rootScope', '$scope', '$state', 'DetailsProvider',function($rootScope, $scope, $state,DetailsProvider) {
    console.log("DetailDashboardController --- ");

    $scope.DetList= DetailsProvider.getDashboardDetails()

}]);

})(window, window.angular);

provider which will call the list

(function(angular) {

var appmod = angular.module('project.DetailsServiceProvider', []);  
appmod.provider('DetailsProvider', function() {
    this.$get = ['_$rest', function DetailServiceFactory(_$rest) {
        return new DetailsProvider(_$rest);
    }];
});

function DetailsProvider(_$rest) {
    this._$rest = _$rest,
    this.getDashboardDetails = function(_callback, _data) {
        var newData = null;
        _$rest.post({
            url: window.localStorage.getItem('contextPath') +'home/listdetail',
            data: {} ,
            onSuccess:_callback
            }
        });

    }
};

})(window.angular);

Thanks in advance for any kind of reply!

1
  • what exactly i want to know is how a function's success value (function in constructor) can pass to the controller? one thing to notice is that the call is in the provider Commented Jul 15, 2016 at 11:15

1 Answer 1

1

You should return promise from your service method and do thenable in your controller.

Root Cause : your are returning the newData which will initalized later after completing the ajax call.Before completing it,you are returning the same variable which will be always null.

In provider,

(function(angular) {

var appmod = angular.module('project.DetailsServiceProvider', []);  
appmod.provider('DetailsProvider', function() {
    this.$get = ['_$rest', function DetailServiceFactory(_$rest) {
        return new DetailsProvider(_$rest);
    }];
});

function DetailsProvider(_$rest) {
    this._$rest = _$rest,
    this.getDashboardDetails = function(_callback, _data) {
        var newData = null;
        _$rest.post({
            url: window.localStorage.getItem('contextPath') +'home/listdetail',
            data: {} ,
            onSuccess:_callback
            }
        });         
    }
};

})(window.angular);

and in controller,

$scope.list = function() {
    DetailsService.getDashboardDetails(function(data){

            varr holdIt = data.data.DList;

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

3 Comments

DetailsService is a provider for me.. _$rest is defined one. should pass as mentioned above.. now i changed the name pls do check with that
u making the things very complicated.Why u need provider.Just u use Service API.Please check the edit
This is the structure have to follow.. i can't change the thing.. and badly stuck.. and i'm new to this . constructor provider and the controller these all are required

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.