我过去做过这个,但这次它一直失败,不知道我哪里出错了。下面是我的文件
应用程序js
(function () {
var app = angular.module("ScmCodeViewer", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/gitlab", {
templateUrl: "gitlab.html",
controller: "gitlabController"
});
});
}());
服务js
(function () {
var app = angular.module("ScmCodeViewer");
app.factory("gitlabService", function ($http) {
....
}
return {
getFiles: getFiles
}
});
});
当我尝试在我的控制器中使用 gitlabService 时,如下所示,我收到“未知提供者”错误
(function () {
var app = angular.module("ScmCodeViewer");
app.controller("gitlabController", function ($scope, $log, gitlabService) {
gitlabService.someMethod();
}
});
}());
但是,如果我将 service js 中的代码移动到 app js 并在那里注册服务,它似乎工作正常。
html
<head>
<script src="scripts/external/angular.min.js"></script>
<script src="scripts/external/angular-route.min.js"></script>
<script src="scripts/app.js"></script>
<script src="scripts/gitLabService.js"></script>
<script src="scripts/gitlabController.js"></script>
</head>
你忘了执行你的服务js。您需要();
在最后确保函数运行。
(function () {
var app = angular.module("ScmCodeViewer");
app.factory("gitlabService", function ($http) {
....
}
return {
getFiles: getFiles
}
});
})(); //right here you need the ()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句