通过routeProvider传递参数解决AngularJS

时间:2016-01-26 19:19:15

标签: angularjs

我对Angular很新,现在我正在按照教程创建一个具有用户授权级别的应用程序。所以我已经建立了服务,我正在尝试在routeProvider应用程序配置上传递权限参数,但它会引发错误。

这是我的代码:

var ngApp = angular.module('neoAutr', ['ngRoute', 'ngMaterial', 'md.data.table', 'LocalStorageModule', 'angular-loading-bar'])
//Permisos de App
.constant('APP_PERMISSIONS', {
    viewHome: "viewHome",
    viewInversions: "viewInversions",
    viewChecks: "viewChecks",
    viewLibrary: "viewLibrary",
    viewOverdrafts: "viewOverdrafts",
    viewOperations: "viewOperations",
    enableUserLevel: "enableUserLevel",
    enableIgnore: "enableIgnore"
})
//Servicio de ruteamiento
.config(['$routeProvider',
  function ($routeProvider) {
      $routeProvider.
        when('/login', {
            templateUrl: 'Scripts/app/views/login.html',
            controller: 'loginController'
        }).
        when('/home', {
            templateUrl: 'Scripts/app/views/home.html',
            controller: 'homeController',
            review: {
                    permissions: [APP_PERMISSIONS.viewHome]
            }
        }).
        when('/cheque', {
            templateUrl: 'Scripts/app/views/cheque.html',
            controller: 'chequeController',
        }).
        when('/info', {
            templateUrl: 'Scripts/app/views/info.html',
            controller: 'infoController'
        }).
        when('/operations', {
            templateUrl: 'Scripts/app/views/operations.html',
            controller: 'operationsController'
        }).
        when('/inversions', {
            templateUrl: 'Scripts/app/views/inversions.html',
            controller: 'inversionsController'
        }).
        when('/overdrafts', {
            templateUrl: 'Scripts/app/views/overdrafts.html',
            controller: 'overdraftsController'
        }).
        otherwise({
            redirectTo: '/login'
        });
  }]);

我在控制台上遇到以下错误:

Uncaught Error: [$injector:modulerr] 

任何人都可以告诉我我做错了什么?

编辑以下是我在index.html上调用的库,希望这可以帮助启发这个问题

<!-- Angular -->
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/angular-local-storage.min.js"></script>
    <script src="Scripts/loading-bar.js"></script>
    <link rel="stylesheet" href="Content/loading-bar.css" />

    <!-- Angular Material -->

    <link rel="stylesheet" href="Content/angular-material.min.css">
    <link rel="stylesheet" href="Content/md-data-table.min.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
    <script src="Scripts/md-data-table.min.js"></script>

    <!-- App-->
    <script src="Scripts/app/config.js"></script>

2 个答案:

答案 0 :(得分:0)

您需要将APP_PERMISSIONS注入config服务,如下所示:

    config(['$routeProvider','APP_PERMISSIONS',
      function ($routeProvider, APP_PERMISSIONS) {
       $routeProvider.
        when('/login', {
            templateUrl: 'Scripts/app/views/login.html',
            controller: 'loginController'
        }).
        when('/home', {
            templateUrl: 'Scripts/app/views/home.html',
            controller: 'homeController',
            review: {
                    permissions: [APP_PERMISSIONS.viewHome]
            }
        }).

这是working example希望这个帮助!

答案 1 :(得分:0)

更改

permissions: [APP_PERMISSIONS.viewHome]

permissions: ['APP_PERMISSIONS', function(APP_PERMISSIONS) {
   return APP_PERMISSIONS.viewHome;
}]

解析值必须是一个返回您想要注入的内容的函数。如果你要缩小你的代码,我使用的语法也会缩小。

请注意,我要求将常量注入resolve函数,而不是模块路由配置函数。