AngularJS错误当前。$$ route未定义

时间:2016-03-04 10:16:17

标签: javascript angularjs

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

  

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

此错误如

  

错误:当前。$$路线未定义

和我的控制器:

'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 =&#34; spaApp&#34;

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

由于

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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