javascript 角度未捕获的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23845679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular Uncaught Object
提问by leaksterrr
I'm new to Angular so i'm still getting my head around how it works. I've stumbled into a problem however (quite early on...) and the below code is giving me "Uncaught Object" in the console and breaks Angular. The .config section is the culprit, if I remove it, the page loads fine. I'm not entirely sure how the error is being caused because to me, everything looks fine?
我是 Angular 的新手,所以我仍在了解它是如何工作的。然而,我偶然发现了一个问题(很早就......),下面的代码在控制台中给了我“未捕获的对象”并破坏了 Angular。.config 部分是罪魁祸首,如果我删除它,页面加载正常。我不完全确定错误是如何引起的,因为对我来说,一切看起来都很好?
var app = angular.module('app', ['ngRoute'])
.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
$routeProvider
.when('/dashboard', {
templateUrl: '/app/views/admin.html',
controller: 'DashboardController'
})
.otherwise('/', {
redirectTo: '/'
})
$locationProvider.html5mode(true);
}])
.controller('DashboardController', ['$scope', function ($scope, Security) {
$scope.security = Security;
}])
回答by cnlevy
I had the same error; if you activate Chrome to pause on exceptions, you'll be able to have more detailed error information
我有同样的错误;如果您激活 Chrome 以暂停异常,您将能够获得更详细的错误信息
回答by callmekatootie
.otherwise
takes only one parameter - an object which contains information on what needs to be done for routes that are not defined.
.otherwise
只接受一个参数 - 一个对象,其中包含有关需要为未定义的路由做什么的信息。
In your case, you seem to be passing a route to it in addition to an object.
在您的情况下,除了对象之外,您似乎还向它传递了一条路线。
Replace:
代替:
.otherwise('/', {
redirectTo: '/'
})
with
和
.otherwise({
redirectTo: '/dashboard'
});
Note that you need to redirect to a path that exists. '/' is a path that does not exist. '/dashboard' is a path that does, hence you redirect to it. Or, define a handler for '/' path
请注意,您需要重定向到存在的路径。'/' 是一个不存在的路径。'/dashboard' 是一个路径,因此你重定向到它。或者,为“/”路径定义一个处理程序
回答by Quv
I'm posting my ownsolution, as several factors seem to be behind this issue and nobody has talked about this so far.
我正在发布我自己的解决方案,因为这个问题背后似乎有几个因素,到目前为止没有人讨论过这个问题。
That is, try to write your code outside of $.ready()
;
也就是说,尝试在$.ready()
;之外编写代码。
<script>
$(function(){
// This leaves "Uncaught object" error in Chrome!
// var app = angular.module('testApp', ['ngRoute']);
});
// So get it out of $.ready()!!
var app = angular.module('testApp', ['ngRoute']);
</script>
回答by leaksterrr
In this instance it looks like the error has been caused by this:
在这种情况下,错误似乎是由以下原因引起的:
$locationProvider.html5mode(true); <-- mode should be capital 'M'.
回答by Thomas
I just ran into this, in my case it was due to using UI-Router and having a duplicated state name.
我刚刚遇到了这个问题,在我的情况下,这是由于使用 UI-Router 并具有重复的状态名称。
回答by Borune
Be sure your angular.js is loaded before angular-route.js in your HTML file. like:
确保 angular.js 在 HTML 文件中的 angular-route.js 之前加载。喜欢:
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular-route.js"></script>