javascript - AngularJS 类型错误 : undefined is not a function on ng-view directive

标签 javascript angularjs ng-view

我正在尝试连接一个简单的 AngularJS 应用程序,但我无法克服 View 指令上的 undefined is not a function 错误。奇怪的是,第一个 View 实际上加载并呈现给指令,但我无法导航到第二个 View 。 Controller 肯定没有运行。我不确定这里发生了什么事。有什么想法吗?

Angular 版本:1.2.26(与 1.2.20 相同的错误)

错误

TypeError: Undefined is not a function

应用代码

var app = angular.module('myApp', ['ngRoute', 'ngResource']);

app.controller('Home', ['$scope', function ($scope) {
    console.log('Home controller hit.');
}]);

app.controller('About', ['$scope', function ($scope) {
    console.log('About controller hit.');
}]);


app.config(function ($routeProvider, $locationProvider) {
    $locationProvider.html5Mode(true);
    $routeProvider.when('/', { templateUrl: 'SiteAssets/views/home.html', controller: 'Home' })
    .when('/home', { templateUrl: 'SiteAssets/views/home.html', controller: 'Home' })
    .when('/about', { templateUrl: 'SiteAssets/views/about.html', controller: 'About' })
    .otherwise({ redirectTo: '/' });
});

最佳答案

您需要将 $document 注入(inject) Controller ,如下所示:

app.controller('About', ['$scope','$document', function ($scope, $document) {
  $document.title = 'About Us';
  console.log('About controller hit.');
}]);

可能是 $document 引发了错误,因为 Angular 在没有注入(inject)的情况下不知道它是什么。但是,此问题的错误将是 Error: Unknown provider:

关于javascript - AngularJS 类型错误 : undefined is not a function on ng-view directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302001/

相关文章:

angularjs - 在指令中使用 controllerAs 语法访问父方法

javascript - 单击按钮时无法删除使用指令创建的表的选定行

javascript - HTML 标题没有并排显示?

javascript - 如何保持动画停止的 linearGradient 位置

javascript - 如何将一个指令的属性值放入AngularJS中的另一个指令中?

angularjs - Angular JS ng-view改变商店滚动位置

html - ng-view 内部的 ng-include 出现 404 问题

javascript - AngularJS:自定义指令内的 ngView

javascript - jquery fadein 不工作?

javascript - 使用 Squirrel 将 Electron App 安装到 ProgramFiles(x86)