简体   繁体   English

控制器未从工厂返回数据

[英]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. 想象一下一个监狱,监狱里有单独的牢房,我希望能够通过调用API'/ getparameters'来返回一个特定的牢房,该API将返回牢房号,然后将该变量作为我工厂的ID传递。 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. 这是对单元格编号进行get ,对结果不执行任何操作,然后将一个函数放入以太坊。

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});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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