繁体   English   中英

$ routeProvider配置路由抛出“未捕获错误:[ng:areq]参数'fn'不是函数,得到了字符串”

[英]$routeProvider config route throwing 'Uncaught Error: [ng:areq] Argument 'fn' is not a function, got string'

我正在使用有角JS的ngRoute编写路由逻辑。 以下是我的代码。

index.js

(function() {
  'use strict';

  function config($routeProvider, $httpProvider, cfpLoadingBarProvider, $tooltipProvider) {
    $routeProvider.otherwise({redirectTo: '/404'});
    $httpProvider.defaults.withCredentials = false;
    $httpProvider.defaults.headers.common['content-type'] = "application/json";
  }

  angular
    .module('pacman', ['ngCookies', 'ngRoute', 'ui.bootstrap', 'ui.validate',
                      'angular-cache', 'angular-loading-bar', 'angular-md5', 'rt.iso8601', 'ngAnimate']
            )
    .config(['$routeProvider', '$httpProvider', 'cfpLoadingBarProvider', '$tooltipProvider', config])
    .run(['$rootScope', '$location', '$modalStack', '$cookies']);
})();

app.controller.js

(function() {
  'use strict';

  function config($routeProvider) {
    $routeProvider.when('/', {
      templateUrl: 'app/components/landingpage/landingpage.html',
      controller: 'appController'
    });
  }

  function appController($scope, $rootScope, $location) {
    $scope.submitLogin = function() {
      alert("Successfully loggedIn");
    };
  }

  angular
    .module('pacman')
    .controller('appController', ['$scope', '$rootScope', '$location', appController])
    .config(['$routeProvider', config]);
})();

notFound.controller.js

(function() {
  'use strict';

  function config($routeProvider) {
    $routeProvider.when('/404', {
      templateUrl: 'app/components/notFound/404page.html',
      controller: 'notFoundController'
    });
  }
  function notFoundController($scope, $rootScope, $location) {
    debugger;
  }
  angular
    .module('pacman')
    .controller('notFoundController', ['$scope', '$rootScope', '$location', notFoundController])
    .config(['$routeProvider', config]);
})();

我的代码是一个简单的应用程序。 我正在尝试根据路线加载不同的控制器。 但是,在加载应用程序时,在最后一个控制器的“ $ routeProvider”中会引发错误

未捕获的错误:[ng:areq]参数'fn'不是函数,得到了字符串http://errors.angularjs.org/1.4.8/ng/areq?p0=fn&p1=not%20a%20function%2C%20got %20string

我不知道如何解决这个问题。 任何线索将不胜感激。 以下是我的图书馆捆绑销售订单。

'node_modules/jquery/dist/jquery.js',
'node_modules/angular/angular.js',
'node_modules/angular-route/angular-route.js',
'node_modules/jquery.transit/jquery.transit.js',
'node_modules/angular-cache/dist/angular-cache.js',
'node_modules/angular-cookies/angular-cookies.js',
'node_modules/angular-loading-bar/build/loading-bar.js',
'node_modules/angular-ui-validate/dist/validate.js',
'node_modules/chart.js/Chart.js',
'node_modules/angular-md5/angular-md5.js',
'node_modules/angular-iso8601/dist/angular-iso8601.js',
'node_modules/angular-animate/angular-animate.js',
'node_modules/angular-chart.js/dist/angular-chart.js',
'node_modules/rx/dist/rx.all.js',
'node_modules/angular-ui-bootstrap/ui-bootstrap-tpls.js',
'node_modules/bootstrap/dist/js/bootstrap.js'

请帮助。

Angular JS文件声明必须在index.html中的jQuery之前

'node_modules /角度/ angular.js',

'node_modules / jquery的/ DIST /的jquery.js',

问题出在index.js ,您可以在其中定义角度应用程序的run方法。

angular
    .module('pacman', []) // removed dependencies for brevity
    .run(['$rootScope', '$location', '$modalStack', '$cookies']);

传递给run的数组中的最后一个参数应该是一个函数,但是您忘记传递一个函数 更改run以添加如下所示的实现,或者如果看不到任何使用,请删除该run

angular.module('pacman', []) // removed dependencies for brevity

       .run(['$rootScope', '$location', '$modalStack', '$cookies',

                function($rootScope,$location,$modalStack,$cookies){
                    // some statements here
            }]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM