角度解析:未定义变量

Angular Resolve: Variable is not defined

本文关键字:未定义 变量      更新时间:2023-09-26

我是第一次使用角度路线,我最近提出了一个关于刷新丢失一些数据的问题,我得到的答案是使用解析。这似乎是我一直在寻找的解决方案,但我无法让它正常工作。

.config(['$routeProvider', function($routeProvider) {
           //determines redirects go via shortcuts, clicking on the management icon on the main page sends the routeProvider /MG which it then uses to pull the relevant HTML file
           $routeProvider
               .when('/MG', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Management'
                       }
                   }
               })
               .when('/PO', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Programs Office'
                       }
                   }
               })
               .when('/SM', {
                   controller: 'teammateController',
                   templateUrl: './assets/departments/department.html',
                   resolve: {
                       activeDepartment: function() {
                           return 'Sales And Marketing'
                       }
                   }
               })
               .when('/', {
                   controller: 'teammateController',
                   templateUrl: 'home.html',
                   resolve: {
                       activeDepartment: function() {
                           console.log("working");
                           return 'home';
                       }
                   }
               })
               .otherwise({
                   controller: 'teammateController',
                   templateUrl: 'home.html',
                   resolve: {
                       activeDepartment: function() {
                           console.log("working");
                           return 'home';
                       }
                   }
               })
       }]);
   app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) {
       $scope.deptName = activeDepartment();
     });

我收到"引用错误:未定义活动部门"。控制台日志工作,我看到"工作",所以我知道正在运行解决方案,但随后出现错误消息。我已经尝试了我能想到的一切,我一辈子都看不到我哪里出了问题。任何帮助将不胜感激。

解析的属性将注入到控制器中,因此请尝试以下操作:

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, activeDepartment) {
   $scope.deptName = activeDepartment;
 });

请注意控制器末尾的附加参数。

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location) {       $scope.deptName = activeDepartment();     });

应该是

app.controller('teammateController', function teammateController($scope, $http, $rootScope, $timeout, Idle, $location, activeDepartment) {       $scope.deptName = activeDepartment;     });
顺便说一句,如果您想

缩小代码,您使用的语法将不起作用。