我正在使用AngularJS 1.6.1,我正在尝试做一个我之前已经做过的很简单的任务(很久以前),但由于某种原因,我无法意识到这次发生的事情。 / p>
基本上我想调用我的REST服务,但是当我在factory
函数的参数中传递resolve
时,它就会停止工作。
我在这里发布了一个简单的例子,以便更容易:
的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>
取代,实际上也没有请求。
答案 0 :(得分:1)
<强>更新强>
它被wiered(因为它没有在控制台中抛出任何错误),但在config
内注入的callApi
依赖关系在任何地方都没有被提及,所以它在那里失败了。删除了unknown
依赖项将解决您的问题。
您的服务方法名称错误。而不是getArticles
来电getResult
。
return callApi.getArticles();
应该是
return callApi.getResult();