未知提供程序错误 - AngularJS 应用程序问题
Unknown provider error - AngularJS App issue
var myApp = angular.module("MyApp", ['ngRoute']);
myApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/cart.php',
controller: 'ctrl',
resolve: {
categories: function(cartService){
console.log('inside resolve categories')
return cartService.getCategories();
}
}
}).
otherwise({
redirectTo: '/'
});
}]);
myApp.controller('ctrl', function (categories, $scope) {
$scope.items = categories;
});
myApp.service("cartService", function ($http, $q) {
this.getCategories = function () {
var deferred = $q.defer();
$http.get('js/categories.js')
.then(function (response) {
deferred.resolve(response.data);
});
return deferred.promise;
}
});
myApp.run(['$rootScope',function($rootScope){
$rootScope.stateIsLoading = false;
$rootScope.$on('$routeChangeStart', function(e, current, pre) {
$rootScope.stateIsLoading = true;
var fullRoute = current.$$route.originalPath;
if(fullRoute == '/')
{
console.log('load categoreis and products')
}
});
$rootScope.$on('$routeChangeSuccess', function() {
$rootScope.stateIsLoading = false;
console.log('route changed');
});
$rootScope.$on('$routeChangeError', function() {
//catch error
});
}]);
我已将 ng-app 和 ng-controller 指令放在 HTML 的顶部
<html lang="en" ng-app="MyApp" ng-controller="ctrl">
但是当我运行 HTML 时,出现以下错误
错误: [$injector:unpr] 未知提供程序: 类别提供程序 <- 类别 <- 控制
我该如何解决这个问题?
编辑:如果我从HTML中删除ng-controller="ctrl",那么没有错误
您收到该错误只是因为您对索引.php和"部分/购物车"使用相同的控制器.php
为"部件/购物车.php"创建一个单独的控制器来解决此问题 问题
代码片段:
// Code goes here
var app = angular.module('app', ['ngRoute']);
app.controller('indexCtrl', function($scope) {
$scope.title = 'Header';
});
app.config(function($routeProvider) {
$routeProvider.when('/', {
template: "<ul><li ng-repeat='item in items'>{{item}}</li></ul>",
controller: 'categoryCtrl',
resolve: {
categories: function(cartService){
return cartService.getCategories();
}
}
});
});
app.controller('categoryCtrl', function (categories, $scope) {
$scope.items = categories;
});
app.service("cartService", function() {
this.getCategories = function() {
return ['A', 'B', 'C'];
};
});
<html ng-app="app">
<head>
<script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script>
<script data-require="angular-route@1.4.2" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular-route.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="indexCtrl">
<h1>{{title}}</h1>
<div ng-view></div>
</body>
</html>
您必须定义注入控制器"ctrl"中的服务/工厂类别。
相关文章:
- 在AngularJS应用程序中使用封装指令和路由的推荐方式是什么
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- 如何在AngularJS应用程序的主体上动态设置溢出
- 如何使用Node.js/MongoDB在AngularJS应用程序中获取特定于目标的数据
- 仅客户端AngularJS应用程序
- 如何使用onDeviceReady设置AngularJS应用程序并初始化Cordova的功能
- 如何在angularjs应用程序中解析Drupal JSON数据
- 一个页面中有多个AngularJS应用程序
- Angularjs应用程序的多个实例
- 在AngularJS应用程序中加载JSON(加载谷歌电子表格)
- 如何将路径更改广播到 AngularJS 应用程序中的所有视图
- AngularJS应用程序中的CORS错误
- AngularJS应用程序:如何将.js文件包含到index.html中
- 无法模块化AngularJS应用程序
- 如何在angularjs应用程序中注销后,通过浏览器后退按钮禁止用户访问上一页
- 如何在Eclipse中运行和调试AngularJS应用程序
- 带有RequireJS的AngularJS应用程序在Safari和Firefox中给出了“使用严格”错误
- 动态加载AngularJS应用程序
- 是否可以将我网站上的普通javascript和css函数包含到我的angularjs应用程序中
- 使用AngularJS应用程序为移动设备(iOS、Android等)构建本地应用程序的最佳方式