AngularJS 错误 current.$$route 未定义

AngularJS error current.$$route is undefined

本文关键字:route 未定义 current 错误 AngularJS      更新时间:2023-09-26

我了解了angularjs。但是我在设置标题时发现了错误。

我的应用.js

'use strict';
var serviceBase = 'http://www.yiiangular.dev/'
var spaApp = angular.module('spaApp', [
  'ngRoute',
  'ModuleTesting',
]);
var spaApp_test = angular.module('ModuleTesting', ['ngRoute']);
spaApp.config(['$routeProvider', function($routeProvider, $locationProvider) {
    $routeProvider.otherwise({
        redirectTo: '/test/index'
    });
}])
.run(['$rootScope', function ($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
        $rootScope.title = current.$$route.title;
    });
}]);

这个错误像

错误:当前.$$route 未定义

和我的控制器:

'use strict';
spaApp_test.config(['$routeProvider', function($routeProvider) {
    $routeProvider
    .when('/test/action1', {
        templateUrl: 'views/book/index.html',
        title: 'Action 1',
        controller: 'action1'
    })
    .when('/test/action2', {
        templateUrl: 'views/book/index.html',
        title: 'Action 2',
        controller: 'action2'
    })
    .when('/test/index', {
        templateUrl: 'views/book/index.html',
        title: 'Action Index',
        controller: 'index'
    })
    .otherwise({
        redirectTo: '/test/index'
    });
}])
.controller('index', ['$scope', '$http', function($scope,$http) {
    $scope.message = 'Index Page';
}])
.controller('action1', ['$scope', '$http', function($scope,$http) {
    $scope.message = 'Action I Page';
}])
.controller('action2', ['$scope', '$http', function($scope,$http) {
    $scope.message = 'Action II Page';
}]);

如何解决此错误?

我已经在我的<html>标签中设置了ng-app="spaApp"。

我错过了什么代码?哦,是的,我使用AngularJS v1.4.7

谢谢

为什么在 <head> 标签中设置它?这意味着 angular 只会在该标签之间运行。如果您希望整个应用程序启用角度,则需要在<html>标签上。

我在 1.4.2 上,所以我们应该很接近。

$$route看起来像是一个内部角度项目。如果您注销$routeChangeSuccess中的当前,您将看到否则路径 $$route 不存在。在这种情况下,有效的方法是只需在您的$routeChangeSuccess中使用current.title,您就会成为黄金。