Thursday 4 May 2017

$http get by id with AngularJS

I'm using AngularJS 1.6.3

I'm trying to get data by id but my angular-side is not working

Server side :

app.get("/user/:id", function(req,res){
    testDb.findOne({_id : req.params.id}, function(err, doc){
        res.send(doc);
        res.statusCode = 200;
    });
});

AngularJS side :

    var UserCtrl = myApp.controller('UserCtrl', function($scope,$http){
    $scope.user = null

    $scope.getByID = function(id) {
            $http.get("/user/" + id).then(function(response){
                console.log("get");
                $scope.user = response.data;
            });
        }

getByID();
    });

I've got an error "id is not defined". But I would like to get it from the URL.



via Tewan

No comments:

Post a Comment