我目前正在处理我的旧项目,以使它们作为一次小型重构练习能够再次完全正常工作,并且遇到了一些不再编译的AngularJS代码。我相信这取决于更新的依赖关系,因为我已经至少三年没有更改此源代码的功能了。
我遇到的问题是:
angular.js:15567错误:[$ injector:unpr]未知提供程序:AProvider <-A <-NavigationCtrl https://errors.angularjs.org/1.7.8/$injector/unpr?p0=AProvider%20%3C-%20A%20%3C-%20NavigationCtrl
at angular.js:138 at angular.js:4924 at Object.getService [as get] (angular.js:5084) at angular.js:4929 at getService (angular.js:5084) at injectionArgs (angular.js:5109) at Object.invoke (angular.js:5133) at $controllerInit (angular.js:11704) at nodeLinkFn (angular.js:10517) at compositeLinkFn (angular.js:9832)
我认为这是注射问题。我正在尝试注入Angular不知道的东西。有点困惑,因为我已经有一段时间没有使用AngularJS了(使用过Angular 2 +)。
我的app.js
文件如下:
angular.module('qaDashboard', ['restangular'])
.run(['$anchorScroll', function ($anchorScroll) {
$anchorScroll.yOffset = 85; // always scroll by 50 extra pixels
}]);
angular.module('qaDashboard').controller('NavigationCtrl', function ($scope, $location, $anchorScroll) {
$scope.scrollTo = function (id) {
$location.hash(id);
$anchorScroll();
};
});
// Environment
require('../components/environments/script.js');
require('../components/environments/style.scss');
// Feature
require('../components/features/script.js');
require('../components/features/style.scss');
// Day
require('../components/days/script.js');
require('../components/days/style.scss');
// Hour
require('../components/hours/script.js');
// Report Data (URL & JSON)
require('../components/report/script.js');
require('../components/report/style.scss');
// Index Style
require('../stylesheets/style.scss');
我的index.html
看起来像这样:
<!DOCTYPE html>
<html ng-app="qaDashboard">
<head>
<title>DASHBOARD</title>
<script src="/node_modules/angular/angular.js"></script>
<script src="/node_modules/lodash/lodash.js"></script>
<script src="/node_modules/restangular/src/restangular.js"></script>
<script src="/javascripts/app.bundle.js" type="text/javascript"></script>
<body>
<div class="navbar">
<p>Regression Test Dashboard</p>
<div ng-controller="NavigationCtrl" class="navbarAlign">
<a ng-click="scrollTo('Dev')" class="btn">Dev</a>
<a ng-click="scrollTo('QA')" class="btn">QA</a>
<a ng-click="scrollTo('Staging')" class="btn">Staging</a>
<a ng-click="scrollTo('Staging_EMEA')" class="btn">Staging EMEA</a>
<a ng-click="scrollTo('Production')" class="btn">Production</a>
<a ng-click="scrollTo('Production_EMEA')" class="btn">Production EMEA</a>
<!--<a class="btn">[Do stuff]</a>-->
</div>
</div>
<environments>
</environments>
</body>
</html>
我尝试删除单个控制器,但没有用。我在这里错过明显的东西吗?自从我上次使用此代码以来,语法/ API发生了很大变化吗?
app,js
文件被webpack捆绑-如果这是问题的根源,则不知道。我知道一个事实,就是这段代码以前工作得很好。我只是觉得我缺少一些琐碎的东西,事实证明这很难发现。
我的package.json
-如果有帮助:
{
"name": "dashboard",
"version": "0.5.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"angular": "^1.7.8",
"body-parser": "~1.19.0",
"concat-map": "0.0.1",
"convert-time": "^0.3.0",
"cookie-parser": "~1.4.4",
"dateformat": "^3.0.3",
"debug": "~4.1.1",
"express": "~4.17.1",
"jenkins-api": "^0.3.1",
"lodash": "^4.17.15",
"morgan": "~1.9.1",
"restangular": "^1.6.1",
"serve-favicon": "~2.5.0"
},
"devDependencies": {
"@babel/core": "^7.5.5",
"acorn": "^6.2.0",
"acorn-dynamic-import": "^4.0.0",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.1.0",
"file-loader": "^4.1.0",
"html-loader": "^0.5.5",
"node-sass": "^4.12.0",
"promise": "^8.0.3",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^2.1.0",
"webpack": "^4.36.1"
}
}
编辑:
我已经尝试过了:
angular.module('qaDashboard', ['restangular'])
.controller('NavigationCtrl', function ($scope, $location, $anchorScroll) {
$scope.scrollTo = function (id) {
$location.hash(id);
$anchorScroll();
}
.run(['$anchorScroll', function ($anchorScroll) {
$anchorScroll.yOffset = 85; // always scroll by 50 extra pixels
}])
});
没有运气。
编辑2:
我只是用以下代码尝试了georgeawg的响应:
angular.module('qaDashboard', ['restangular'])
.controller('NavigationCtrl', ["$scope", "$location", "$anchorScroll",
function ($scope, $location, $anchorScroll) {
$scope.scrollTo = function (id) {
$location.hash(id);
$anchorScroll();
}
}]);
// Environment
require('../components/environments/script.js');
require('../components/environments/style.scss');
// Feature
require('../components/features/script.js');
require('../components/features/style.scss');
// Day
require('../components/days/script.js');
require('../components/days/style.scss');
// Hour
require('../components/hours/script.js');
// Report Data (URL & JSON)
require('../components/report/script.js');
require('../components/report/style.scss');
// Index Style
require('../stylesheets/style.scss');
我现在遇到一个angular.js:15567 Error: [$injector:unpr] Unknown provider: AProvider <- A
问题。
我的组件如下所示:
angular.module('qaDashboard').component('environments', {
controller: function (Restangular) {
this.$onInit = () => {
Restangular.one('environments').get().then((response) => {
this.environments = response.environments;
});
}
},
template: require('./template.html'),
}).filter('formattedEnvironment', () => {
return (item) => {
return item.replace('-', ' ')
.replace('_', ' ')
.replace('_', ' ')
.replace('_', ' ');
}
});
<div id="{{environment}}" ng-repeat="environment in $ctrl.environments" class="borderedHolder">
<h1>{{environment | formattedEnvironment}}</h1>
<features environment='environment' >
</features>
</div>
(有多个组件,它们都使用Restangular
,就像上面的^
编辑3:我已经尝试过:
具有:angular.js:15567 Error: [$controller:ctrlreg] The controller with the name 'EnvironmentsController' is not registered.
angular.module('qaDashboard').component('environments', {
controller: 'EnvironmentsController', ['Restangular', function (Restangular) {
this.$onInit = () => {
Restangular.one('environments').get().then((response) => {
this.environments = response.environments;
});
}
},
template]: require('./template.html'),
})
答案 0 :(得分:0)
angular.module('qaDashboard', ['restangular'])
.controller('NavigationCtrl', ["$scope","$location","$anchorScroll",
function($scope, $location, $anchorScroll) {
$scope.scrollTo = function (id) {
$location.hash(id);
$anchorScroll();
}
}]);
为帮助您在丑陋之前发现此类问题,请使用严格依赖注入。
从文档中:
使用严格依赖注入
您可以在与
ng-strict-di
相同的元素上添加ng-app
指令,以选择进入严格的DI模式:<!doctype html> <html ng-app="myApp" ng-strict-di> <body> I can add: {{ 1 + 2 }}. <script src="angular.js"></script> </body> </html>
每当服务尝试使用隐式注释时,严格模式都会引发错误。
有关更多信息,请参见
请您根据我的修改3向我展示?
app.component('environments', {
̶c̶o̶n̶t̶r̶o̶l̶l̶e̶r̶:̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶R̶e̶s̶t̶a̶n̶g̶u̶l̶a̶r̶)̶ ̶{̶
controller: ["Restangular", function(Restangular) {
this.$onInit = () => {
Restangular.one('environments').get().then((response) => {
this.environments = response.environments;
});
}
̶}̶,̶
}],
template: require('./template.html'),
})
从文档中:
隐式注释
注意::如果您打算minify代码,则您的服务名称将被重命名并破坏您的应用程序。
有关更多信息,请参见