简体   繁体   中英

Controller not returning data from factory

Imagine a prison with prisoners in separate cells, and I want to be able to be able to access a specific cell by calling the API '/getparameters' that will return the cell number, and then pass that variable in as the id for my factory. This is what I have come up with. No error are returned, but no data is being returned either. Where am I going wrong?

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

//app.factory('Params', function)
app.factory ('CellID', function($resource){
    return $resource('/my/api/cell/:id');
});

app.controller('CellCtrl', function($scope, $http, CellID){
    //use $http to getparameters, specifically, the cell number
    $http.get('/getparameters')
    .success(
        function(data) {
            var cellnumber = data.cellnum; 
            CellID.get({id:cellnumber}), function(data){
                $scope.info = data;
            }
        }
    ) 
});

You've got your parentheses in the wrong places:

CellID.get({id:cellnumber}), function(data){
    $scope.info = data;
}

This is doing a get with the cellnumber, doing nothing with the result, and then just dropping a function into the ether.

I think you meant to do this:

CellID.get({id:cellnumber}/* <-- no parenthesis here */, function(data){
    $scope.info = data;
}); // <-- parenthesis here (and a semicolon)

You should also be able to just do this:

$scope.info = CellID.get({id:cellnumber});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM