View mysql data using Angularjs/codeigniter

Asked

Viewed 265 times

1

I am developing an application in Codeigniter 3 and came across the following problem. I want to import the database table data and display in a php page.

In my controller I created the function:

public function relacionar() {
    $query = $this->produto->listagem();
     return json_encode($query->result());
}

In the product.js file I created the following Factory:

app.factory('ProdutoService', ['$http', '$q', function ($http, $q) {

        return {
            retornaDados: function () {
                var deffered = $q.defer();
                $http({url: window.location.origin + '/ged/produto/relacionar', method: 'GET'})
                        .then(function (response) {
                            deffered.resolve(response.data);
                        });

                return deffered.promise;
            }

        };
    }]);

I want to add the return to the $Scope.list variable, so I proceeded as follows:

 function ($scope, $http, $log, $timeout, ProdutoService) {
        $scope.list = function () {
            ProdutoService.retornaDados().then(function (retorno) {
                return retorno;  
            });
        };

Instead of getting the answer that came from the database, Return receives the function and not the data. Follow the return:

 function () {
            ProdutoService.retornaDados().then(function (retorno) {
                console.log(retorno);  
            });
        }

What should be the right way to get the data and not the return function? The fact of using codeIgniter may be interfering with the data?

Obs: I have tested the function that fetches the data in the database and it is returning a json array.

1 answer

0

Your code is returning the return of the function, to the function Productosservice.returnedDados(). I believe a Return earlier would work:

 function ($scope, $http, $log, $timeout, ProdutoService) {
        $scope.list = function () {
            return ProdutoService.retornaDados()
            .then(function (retorno) {
                return retorno;  
            });
        };

Browser other questions tagged

You are not signed in. Login or sign up in order to post.