0

Follow the code

server.js

    app.get('/list',function(req,res){

        console.log('/list api run')

        var data=[
    { "isActive": false, "balance": "$3,960.64", "age": 30, "eyeColor": "blue", "name": "Dawn Keith", "gender": "female", "company": "COSMOSIS", "email": "[email protected]", "phone": "+1 (839) 437-3421", "address": "392 Clifford Place, Fontanelle, Arizona, 2687"
    },
    { "isActive": false, "balance": "$1,280.14", "age": 31, "eyeColor": "green", "name": "Bettie Eaton", "gender": "female", "company": "COMTREK", "email": "[email protected]", "phone": "+1 (861) 460-2317", "address": "203 Allen Avenue, Elrama, North Carolina, 4453"
    },
    { "isActive": true, "balance": "$2,042.37", "age": 38, "eyeColor": "green", "name": "Margie Ayala", "gender": "female", "company": "VOIPA", "email": "[email protected]", "phone": "+1 (941) 569-2231", "address": "111 Schroeders Avenue, Suitland, Louisiana, 7042"
    },
    { "isActive": false, "balance": "$3,170.35", "age": 37, "eyeColor": "blue", "name": "Baker Townsend", "gender": "male", "company": "EVIDENDS", "email": "[email protected]", "phone": "+1 (808) 500-2793", "address": "190 Just Court, Canoochee, Alabama, 325"
    }]

    res.json(data);
    })

controller.js

    var app = angular.module('appList', []);


    app.factory('contactService', function($http){
        var contactResponse = {};

        contactResponse.list=function(){
            return $http({

                method: "GET",
                url: "/list"

            })
        }

        return contactResponse; 
    })

    app.controller('contacCtrl', function($scope,contactService){

    $scope.listContact=function(){
        contactService.list().then(function(response){
            $scope.setcontact=response;
        })
    }

    })

Using 'success', it worked, but since this method was taken from the 1.6 api, I started using 'then', which by the way returns only 'NaN and 200'

2 Answers 2

2

Then will return raw Http Response from the server rather than parsed data. You should change to response.data

$scope.listContact=function(){
        contactService.list().then(function(response){
            $scope.setcontact=response.data;
        })
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The data you're looking for is totally in the response you got! You just don't know it.

Getting a response 200 means that it went alright, and everything that was supposed to go through, did correctly. And in that response, there is a field called response.data that contains what you're looking for.

source: https://docs.angularjs.org/api/ng/service/$http

Comments

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.