简体   繁体   English

Angular $ http在$ routeProvider中无法解析

[英]Angular $http not resolving in $routeProvider

I am trying to display data from a JSON file onto the webpage. 我正在尝试将JSON文件中的数据显示到网页上。 My Code 我的密码

angular.module('bioA.configs',['ngRoute'])
//Config routes
.config(['$routeProvider',function($routeProvider){
    $routeProvider.when('/',
    {
        templateUrl: 'list.tpl.html',
        controller: 'BioACtrl',
        resolve: {
            bioAList: ['$http',function($http){
                return $http.get('bioa.json');
            }]
        }
    });
}]);
//Controller to Manage the data
angular.module('bioA.controllers',['bioA.configs'])
.controller('BioACtrl',['bioAList','$scope',function($scope,bioAList){
    console.log(bioAList);
    $scope.samples = bioAList.data.samples;
}]);
angular.module('bioA',['ngRoute','bioA.controllers','bioA.configs','ui.bootstrap']);

The $http doesnt seem to resolve. $ http似乎没有解决。 here is the console output: 这是控制台输出: 控制台输出 I am AngularJS noob any help is appreciated :) I am stuck. 我是AngularJS新手,不胜感激:)我被卡住了。 Why is the resolved object a ChildScope rather than being a promise ? 为什么解决的对象是ChildScope而不是诺言?

first of all, your includes are reversed here 首先,这里包含的内容被颠倒了

controller('BioACtrl',['bioAList','$scope',function($scope,bioAList){

Should be 应该

controller('BioACtrl',['$scope', 'bioAList',function($scope,bioAList){

Second, you are trying to access data in your bioAList service before you even fetch it. 其次,您甚至试图在bioAList服务中访问数据。 The correct way to do this is with angular promises. 正确的方法是保证承诺。 I modified the plnkr to acheive this access paradigm: 我修改了plnkr以实现这种访问方式:

bioAList.getSamples().then(function(data) {
    $scope.samples = data.samples;
})

EDIT: add the thing that @OdeToCode points out. 编辑:添加@OdeToCode指出的内容。

Good point! 好点子! you can just return the $http promise as the service. 您只需将$ http诺言作为服务返回即可。

return $http.get('bioa.json').success(function(data,status){
    return data;
})

And access it like you originally had. 并像原来一样访问它。

Hope this helps! 希望这可以帮助!

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

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