AngularJs - 路由解析功能无法识别工厂

AngularJs - route resolve function does not recognize factory

我正在使用 AngularJS 1.6.1,我正在尝试做一个我以前(很久以前)做过的简单任务,但出于某种原因我无法理解是什么这次正在发生。

基本上我想调用我的 REST 服务,但是当我在 resolve 函数的参数中传递我的 factory 时,它停止工作。

我在这里给 post 做了一个简单的例子,以使其更容易:

index.html

<html ng-app="TesteApp">
<head>
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>

  <script src="app.js"></script>
</head>
<body>
    <div class="ng-view">
    </div>
</body>

view.html

<h1>DONE</h1>

app.js

angular.module('TesteApp', ['ngRoute']);

angular.module('TesteApp').controller('testeController', function($scope, resultSet) {
   console.log("1");
});

angular.module('TesteApp').config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "view.html",
        controller: "testeController",
        resolve: {
            resultSet: function(callApi) { // seems like 'callApi' is not being recognized?!
                //If I return the fixed list below and take the 'callApi' parameter above, it works.
                //return "[{id:1, nome: 'teste'}, {id:2, nome: 'teste2'}]";
                return callApi.getResult(); 
            }
        }
    });
});

angular.module("TesteApp").factory("callApi", function ($http, config) {
    var _getResult = function () {
        return $http.get("http://localhost:8080/result");
    };

    return {
        getResult: _getResult
    };
});

正如我在评论中所说,似乎无法识别 'callApi'。

控制台不打印错误,<div class="ng-view">没有被<h1>DONE</h1>替换,实际上没有请求完成。

更新

它很奇怪(因为它没有在控制台中抛出任何错误),但是在 callApi 中注入的 config 依赖项没有在任何地方提到,所以它在那里失败了。删除 unknown 依赖项将解决您的问题。

Demo Here


您输入的服务方法名称有误。而不是 getArticles 调用 getResult.

return callApi.getArticles();

应该是

return callApi.getResult();