我正在尝试连接一个简单的 AngularJS 应用程序,但我无法克服 View 指令上的 undefined is not a function
错误。奇怪的是,第一个 View 实际上加载并呈现给指令,但我无法导航到第二个 View 。 Controller 肯定没有运行。我不确定这里发生了什么事。有什么想法吗?
Angular 版本:1.2.26(与 1.2.20 相同的错误)
错误
应用代码
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/