【问题标题】:$rootscope throwing error in angular Js$rootscope 在角度 Js 中抛出错误
【发布时间】:2018-11-14 22:16:00
【问题描述】:

我正在使用 Angular JS 练习 $rootscope。这里我定义rootscope如下

var sample=angular.module("sample",[]).run(['$rootscope', function($rootScope){
            $rootScope.months=12;
        }]) 

这个根作用域在内部作用域中调用如下

sample.controller('salary', ['$scope','$rootscope', function($rootScope,$scope){
            $scope.dept="developer";
            $scope.salary=58900
            $scope.getAnualSalary=function () {
                return (this.salary)*$rootScope.months;
            }
        }])

但我没有显示想要的结果并在控制台中抛出错误

angular.js:14800 错误:[$injector:unpr] http://errors.angularjs.org/1.6.9/$injector/unpr?p0=%24rootscopeProvider%20%3C-%20%24rootscope%20%3C-%20salary

full code is here

我的代码有什么问题?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    改变顺序。您需要在参数和字符串值中具有相同的顺序

    sample.controller('salary', ['$scope','$rootScope', function($scope, $rootScope){
    

    【讨论】:

    • 你把rootscope改成rootScope了吗
    • 在您的方法中,您可能希望返回 scope.salary。 return ($scope.salary)*$rootScope.months;
    • 我认为 Angular js 不区分大小写,我更改它工作正常。感谢指正
    【解决方案2】:

    正确的注入器是 $rootScope(区分大小写)

    var sample=angular.module("sample",[]).run(['$rootScope', function($rootScope){
    

    字符串和函数参数的顺序也应该相同

    sample.controller('salary', ['$scope','$rootScope', function($scope, $rootScope){
    

    【讨论】:

      猜你喜欢
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多