Angularjs 如何通过Mongodb数据库在angular js中使用$http服务按名字获取用户
您好,我正在尝试从我的mongoDB数据库中获取单个用户 我提供的服务如下: 服务:Angularjs 如何通过Mongodb数据库在angular js中使用$http服务按名字获取用户,angularjs,Angularjs,您好,我正在尝试从我的mongoDB数据库中获取单个用户 我提供的服务如下: 服务: app.service('UsersService', function($http, $routeParams) { var getAllUrl = '/users'; this.getSingleUser = function() { return $http.get(getAllUrl/$routeParams.firstname) .then(function(
app.service('UsersService', function($http, $routeParams) {
var getAllUrl = '/users';
this.getSingleUser = function() {
return $http.get(getAllUrl/$routeParams.firstname)
.then(function(response) {
return response.data;
})
}
});
app.controller('singleContactCtrl',
function($scope, UsersService, $routeParams) {
// Get single contact
UsersService.getSingleUser().then(function(data) {
$scope.singlecontact = data;
});
});
然后是控制器
控制器:
app.service('UsersService', function($http, $routeParams) {
var getAllUrl = '/users';
this.getSingleUser = function() {
return $http.get(getAllUrl/$routeParams.firstname)
.then(function(response) {
return response.data;
})
}
});
app.controller('singleContactCtrl',
function($scope, UsersService, $routeParams) {
// Get single contact
UsersService.getSingleUser().then(function(data) {
$scope.singlecontact = data;
});
});
在我的api上,当我执行/users/firstname时,我会得到我的json数据。我做错什么了吗?使用该资源非常简单,但我想进一步了解$http的工作原理 我认为问题在于url
getAllUrl/$routeParams。firstname
应该改为getAllUrl+'/'+$routeParams。firstname
我认为问题在于urlgetAllUrl/$routeParams。firstname
应该改为getAllUrl+'/'+$routeParams。firstname
你能做什么$scope.singlecontact=JSON.parse(数据);?非常感谢。我也试过了,但没有效果。返回404。这里至少有3处错误。1) $http.get需要一个字符串参数,您的参数是一个变量除以另一个变量。2) “getSingleUser”方法应该有一个参数(firstname),您应该使用arg从控制器调用该方法。首先试试这两件事。你能做$scope.singlecontact=JSON.parse(data);?非常感谢。我也试过了,但没有效果。返回404。这里至少有3处错误。1) $http.get需要一个字符串参数,您的参数是一个变量除以另一个变量。2) “getSingleUser”方法应该有一个参数(firstname),您应该使用arg从控制器调用该方法。首先试试这两件事。非常感谢这正是问题所在。非常感谢这正是问题所在