AngularJS:“;TypeError:undefined不是函数“”;使用routeProvider
AngularJS: "TypeError: undefined is not a function" with routeProvider
我正在试图追踪AngularJS中的"TypeError:undefined不是函数"错误。如果您对如何调试类似的东西有任何想法,甚至更好的建议,我将不胜感激。请注意,这与我正在处理的代码非常相似,但并不完全相同(尽管运行时仍然有相同的错误)。
跟踪:
TypeError: undefined is not a function
at update (http://localhost:63342/Channels/vendor/angular-route.js:838:13)
at Scope.$broadcast (http://localhost:63342/Channels/vendor/angular.js:11803:28)
at http://localhost:63342/Channels/vendor/angular-route.js:549:26
at wrappedCallback (http://localhost:63342/Channels/vendor/angular.js:10549:81)
at wrappedCallback (http://localhost:63342/Channels/vendor/angular.js:10549:81)
at http://localhost:63342/Channels/vendor/angular.js:10635:26
at Scope.$eval (http://localhost:63342/Channels/vendor/angular.js:11528:28)
at Scope.$digest (http://localhost:63342/Channels/vendor/angular.js:11373:31)
at Scope.$apply (http://localhost:63342/Channels/vendor/angular.js:11634:24)
at done (http://localhost:63342/Channels/vendor/angular.js:7635:45)
index.html:
<!DOCTYPE html>
<html ng-app="channelsApp">
<head>
<title></title>
<link rel="stylesheet" href="css/style.css"/>
<script src="vendor/angular.js"></script>
<script src="vendor/angular-route.js"></script>
</head>
<body>
<div ng-view></div>
<!--App Scripts-->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
</body>
</html>
app.js
var channelsApp = angular.module('channelsApp', [
'ngRoute',
'channelsControllers'
]);
channelsApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/new', {
templateUrl: 'partials/test.html',
controller: 'CreateChannelCtrl'
}).
otherwise({
redirectTo: '/new'
});
}]);
controllers.js:
'use strict';
/* Controllers */
var channelsControllers = angular.module('channelsControllers', []);
channelsControllers.controller("CreateChannelCtrl", ['$scope',
function($scope) {
console.log("Success!");
}]);
test.html
<div>This is a test.</div>
使用过时的AngularJS版本会导致此错误。
我使用了相同的代码,它对我有效。库可能有一些问题。更新角度库。如果你想要我的代码,请告诉我。
我不认为<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
,因为在他们网站上的AngularJS教程中,他们也在controller.js之前包含了app.js。我想这是合乎逻辑的,因为在javascript和"严格模式"中,你也可以调用一个函数,然后定义它;
<html>
<body>
<script>
'use strict';
callMe(0);
function callMe(a){alert(a)}
</script>
</body>
</html>
上面的代码运行良好。
如果我在任何时候都错了,请纠正我。
谢谢。
由于channelsApp
依赖于channelsControllers
var channelsApp = angular.module('channelsApp', [
'ngRoute',
'channelsControllers'
]);
你必须像这样反转脚本:
<script src="js/controllers.js"></script>
<script src="js/app.js"></script>
可能的问题是你使用不同版本的angular.js和angular-route.js。正如这个演示所示,当它们的版本不同时,就会出现错误。
如果你使用相同的版本,比如这个DEMO,它是有效的。
注意:ngRoute从1.2版本开始被移到自己的模块中。在1.0版本中,ngRoute是核心。有关详细信息:http://docs.angularjs.org/guide/migration#ngroute-已移动到其自己的模块
- Angularjs:如何在使用'解决'在$routeProvider中
- 使用routeProvider中的服务属性
- AngularJS:“;TypeError:undefined不是函数“”;使用routeProvider
- 使用$routeProvider进行动态路由和编译角度代码
- 使用'解决'以等待routeProvider中的查询结果
- AngularJS-如何在配置函数中使用$routeProvider和自定义模块
- 如何使用$routeProvider在Angular中更改页面标题
- AngularJS's$routeProvider templateUrl总是使用Express返回404
- 如何使用$routeProvider访问Angular ng视图中的Javascript元素
- Angular、多个控制器和使用$routeProvider进行路由
- 使用$routeProvider的Angular路由
- AngularJS -拒绝了使用$routeProvider:resolve的$http承诺
- 如何使用angularJS $routeProvider和$routeParams
- 如何使用angularjs的routeProvider重定向到外部URL ?
- 在click事件中使用angular js调用routeProvider
- 在使用了ngRoute并且包含了angular-route.js的情况下,也没有定义routeProvider
- 在另一个函数中使用$routeProvider resolve函数
- Angularjs使用$routeProvider而不使用ng-view
- url 中不需要的哈希“#”,在 angularjs 中使用$routeProvider
- 在AngularJS中使用$routeProvider将控制器分配给表单模板