[英]Angular unknown provider when trying to get a constant from another module
Just started on AngularJS, and it has been a challenging ordeal so far. 刚开始使用AngularJS,到目前为止,它一直是一项艰巨的考验。
My problem today is I'm trying to configure a controller through a variable on the URL. 我今天的问题是我试图通过URL上的变量配置控制器。 I don't want the "real" controller to have to know where a given parameter came from, so long as it's there. 我不希望“真实的”控制器必须知道给定参数的来源,只要它在那里。 The main app controller is therefore responsible of getting the parameter from the URL, and setting a constant that the "real" controller will use. 因此,主应用程序控制器负责从URL获取参数,并设置“真实”控制器将使用的常量。
For the life of me, I cannot see what I am doing wrong in the initialization. 对于我的一生,我看不到初始化中做错了什么。 Any help would be greatly appreciated (including what are standard practices to troubleshoot these kind of issues :)) 任何帮助将不胜感激(包括解决此类问题的标准做法:)
Here is the html: 这是html:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<!-- the base tag is required for th Angular.js $location.search() function to work correctly -->
<base href='/' />
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.js"></script>
<script>
angular.module("myController.constants", []);
angular.module("myApp", ["myController", "myController.constants"], function($locationProvider) {
$locationProvider.html5Mode(true);
})
.controller("myAppCtrl", ['$scope', '$location', function ($scope, $location) {
var searchObject = $location.search();
angular.module("myController.constants").constant('myConstant', searchObject['theConstant']);
}]);
</script>
<script src="js/controllerExample.js"></script>
</head>
<body ng-controller="myAppCtrl">
<div ng-controller="myControllerCtrl">
<p>The constant is {{theConstant}}</p>
</div>
</body>
</html>
Here is the js for the controller: 这是控制器的js:
angular.module("myController", ["myController.constants"])
.controller("myControllerCtrl", ['$scope', 'myConstant', function ($scope, myConstant) {
$scope.theConstant = myConstant;
}]);
With the code above, I keep getting this error message 使用上面的代码,我不断收到此错误消息
Error: [$injector:unpr] Unknown provider: myConstantProvider <- myConstant <- myControllerCtrl 错误:[$ injector:unpr]未知提供程序:myConstantProvider <-myConstant <-myControllerCtrl
Thanks! 谢谢!
I could be mistaken but I don't think you can declare a module inside a controller declaration. 我可能会误会,但我认为您不能在控制器声明中声明模块。 Try putting 试穿
angular.module("myController.constants").constant('myConstant', searchObject['theConstant']);
outside "myAppCtrl" declaration. 在“ myAppCtrl”声明之外。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.