gpt4 book ai didi

javascript - 为什么我不在 Angular Controller 中注入(inject)服务?

转载 作者:行者123 更新时间:2023-12-03 02:32:32 25 4
gpt4 key购买 nike

我尝试将我的服务注入(inject) mi Controller ,但不起作用,并且 chrome 控制台显示此错误:

angular.min.js:124 Error: [$injector:unpr] http://errors.angularjs.org/1.6.6/$injector/unpr?p0=bookServiceProvider%20%3C-%20bookService%20%3C-%20bookController

我的html:

<html ng-app="myApp">
<head>
<script src="resources/jquery/jquery.min.js"></script>
<script src="resources/angular/angular.min.js"></script>
<script src="services/book.service.js"></script>
<script src="controllers/book.controller.js"></script>
</head>
<body ng-controller="bookController">

</body>

我的 Controller :

(function () {
'use strict';

angular
.module('myApp', [])
.controller('bookController', bookController);

bookController.$inject = ['$location', '$rootScope', '$scope', 'bookService'];

function bookController($location, $rootScope, $scope, bookService) {
var vm = this;


}
})();

当我尝试注入(inject)一些库(如 toastr、其他服务等)时,就会发生这种情况......

最佳答案

似乎您正在 book.controller.js 脚本中定义应用程序模块

以下部分创建一个新模块:

angular.module('myApp', []); // Creates and returns the "myApp" module

所以我怀疑您的 bookService 是否在同一个模块中声明。

如果您已经有一个名为“myApp”的模块,则只需将 Controller 附加到它即可:

angular.module('myApp'); // Returns a previously created module named "myApp"

关于javascript - 为什么我不在 Angular Controller 中注入(inject)服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48674399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com