Mean Stack - 从控制器到$ resource对象的Angular传递参数

时间:2016-08-31 00:30:59

标签: javascript angularjs get mean-stack angular-resource

我已经发布了几次认为我的问题是将参数传递给Express服务器。我已经将问题显示在Angular方面。当我在flConstruct.js函数中硬编码id时,查询按预期工作。看起来id没有从flCachedConstructs函数传递到flConstruct中的“get”。正如您所看到的,我使用控制台日志在多个位置打印出id,并打印出正确的id。由于某种原因,该参数未与flConstruct中的get相关联。也许我的间距或语法不太正确。您可以提供的任何见解将不胜感激。

// flConstructDetailCtrl.js
angular.module('app').controller('flConstructDetailCtrl', function($scope, flCachedConstructs, $stateParams, $state, flConstructDataService, flQuestionQueryParameterService){

console.log('Just inside the flConstructDetailCtr');
console.log('flConstructDetailCtr - before promise - $stateParams.id ' + $stateParams.id);


    flCachedConstructs.queryID($stateParams.id)
    .then(function(data) {
        // work with data here
        console.log("flConstructDetailCtr - Get Success " + data);
        console.log("flConstructDetailCtr - Get Success Question description " + data.description);
        console.log("flConstructDetailCtr - Get Success Question id " + data._id);
        flConstructDataService.setInitialized(true);

        flConstructDataService.setNewConstructId(data._id);
        flConstructDataService.setNewTitle(data.title);
        flConstructDataService.setNewDescription(data.description);
        $state.go("constructUpdate");
    });


//flCachedConstructs.js

angular.module('app').factory('flCachedConstructs', function(flConstruct,$http, $q, $state, $timeout) {
var constructList = null;  // temporarily set to null for testing purposes

return {
    queryID: function(id) {
        console.log("flCachedConstruct - queryID function by ID - start");
        console.log("flCachedConstruct - queryID function by ID - parameter = constructId " + id);
        var deferred = $q.defer();
        flConstruct.get(id, function(data) {
            if (data) {
                deferred.resolve(data);
                console.log("flCachedConstruct - queryID function by ID - resolved Data = " + data);
                console.log("flCachedConstruct - queryID function by ID - resolved Data data.description " + data.description);
                console.log("flCachedConstruct - queryID function by ID - resolved Data data._id " + data._id);
            }
            else {
                deferred.reject("Error getting Construct");
                console.log("flCachedConstruct - queryID function by ID - rejected Data");
            }
        });
        console.log("flCachedConstruct - queryID function by ID - return Promise data");
        return deferred.promise;
    },

}
})


//flConstruct.js

angular.module('app').factory('flConstruct',function($resource){
var ConstructResource = $resource('/api/constructs/:id', {id: '@id'}, {
    get: { method: 'GET', url: '/api/constructs/byId', params: {id: '@id'}},
//get: { method: 'GET', url: '/api/constructs/byId', params: {id: '570ab7c086ccc2d81ac18fa6'}},  (By hard coding the query worked as expected)

    query: { method: 'GET' , url: '/api/constructs', isArray: true },
    create: { method: 'POST'},
    update: { method: 'PUT' },
    delete: { method: 'DELETE', params: {id: '@id'}}
});

return ConstructResource;
});

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误。在flCachedConstruct.js中对get进行的调用没有正确定义要传递的参数。我需要包含参数名称“id”以及参数值constructID。此函数调用中的“id”与我在flConstruct.js中调用的参数相匹配。

flConstruct.get({id:constructId},function(data){