简体   繁体   English

尝试从另一个模块获取常量时,角度未知的提供程序

[英]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 <-m​​yConstant <-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.

相关问题 尝试从另一个模块插入服务时出现未知的提供程序错误 - Unknown provider error when trying to inject a service from another module Angular - 来自提供商的未知提供商 - Angular - Unknown Provider from Provider 未知提供程序:尝试使用Angular ngStorage时ngStorageProvider &lt;-ngStorage - Unknown provider: ngStorageProvider <- ngStorage when trying to use Angular ngStorage 尝试在Angular控制器上注入依赖项时出现未知提供程序错误 - Unknown provider Error when trying to inject dependency on Angular controller 在Angular.js中,尝试添加动画模块时遇到了未知的服务提供商。 是否因为我定义的模块没有“ var =”? - In Angular.js I am getting unknown service provider when trying to add the animation module. Is it because I am defining modules without “var =”? 如何从Angular.js中的另一个模块注入提供程序 - How to inject provider from another module in Angular.js 从外部模块将提供程序注入配置块时发生未知的提供程序错误 - Unknown Provider error when injecting provider from a foreign module into config block 尝试使用$ cookies时出现未知提供程序错误 - Get Unknown Provider Error trying to use $cookies 尝试包含ngMaterial $ mdToast时,Angular Material“[$ injector:unpr]未知提供程序”错误,$ animate - Angular Material “[$injector:unpr] Unknown provider” errors when trying to include ngMaterial $mdToast, $animate 注入常​​量时,未知提供程序的错误抛出 - Unknown provider's error throw when injecting constant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM