简体   繁体   中英

AngularJS error current.$$route is undefined

I've learn about angularjs. But I found error when i set a title.

My App.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;
    });
}]);

This error like

Error: current.$$route is undefined

and My controller :

'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';
}]);

How to make this error solve??

I already set ng-app="spaApp" in my <html> tag also.

What is the code I have miss? Oh ya, I use AngularJS v1.4.7

Thanks

Why is it set in the <head> tag? That means that angular will only operate in between that tag. If you want your whole app to be angular enabled then it would need to go on the <html> tag.

I'm on 1.4.2 so we should be close.

$$route looks like it's kind of an internal angular item. If you log out current in your $routeChangeSuccess you'll see that for otherwise paths $$route doesn't exist. What works in this case is to just use current.title in your $routeChangeSuccess and you'll be golden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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