如何将多个参数从控制器传递到资源

时间:2016-08-22 16:38:17

标签: angularjs node.js ionic-framework

在我的控制器中

 API.logInCheck().get({ nid: $scope.username, pwd: $scope.password, comname: $scope.comtyname }).$promise.then(function(response) {
            $rootScope.popup('success', JSON.stringify(response));
        }, function(err) {
            $rootScope.popup('err', JSON.stringify(err));
        });

在我的服务中

 return $resource(base + '/user/:nid/:pwd/:comname', { nid: 'nid', pwd: 'pwd', comname: 'comname' });

在我的服务器

router.route('/user/:nid/:pwd/:comname')
.get(function(req, res) {
Community.findOne({ community_name: req.params.comname }, function(err, commu) {
    if (err)
        res.json(err);
    else {

        User.findOne({ username: req.params.nid, password: req.params.pwd, comty_id: commu._id }, function(err, user) {
            if (err)
                res.json(err);
                res.json(user);
        });

    }
});

但它返回一个数组! 我希望网址

/user/123/123456/text

怎么样?

1 个答案:

答案 0 :(得分:0)

你必须在$ ressouce中使用@:

return $resource(base + '/user/:nid/:pwd/:comname', { nid: '@nid', pwd: '@pwd', comname: '@comname' });

您的电话:

API.logInCheck().query({ nid: $scope.username, pwd: $scope.password, comname: $scope.comtyname }).$promise.then(function(response) {
            $rootScope.popup('success', JSON.stringify(response));
        }, function(err) {
            $rootScope.popup('err', JSON.stringify(err));
        });