[英]Angular abstract state not working on page reload: “App already bootstrapped with this element”
I'm building my app on top of meanjs.org application. 我正在meanjs.org应用程序之上构建我的应用程序。
I needed a main controller on top of each pages so I updated a bit the stateslike this: 我需要在每个页面的顶部都有一个主控制器,所以我更新了如下状态:
$stateProvider
.state('app', {
abstract: true,
templateUrl: 'modules/core/client/views/core.client.view.html',
controller: 'MainController',
controllerAs: 'mc'
})
.state('app.home', {
url: '/',
templateUrl: '/modules/core/client/views/home.client.view.html',
controller: 'HomeController',
controllerAs: 'vm'
})
.etc...
Everything worked as expected until I started to work on the authentication page. 一切正常,直到我开始在身份验证页面上工作。
What I did is just to add the main abstract app.
我所做的只是添加主要的抽象
app.
to the authentication abstract to keep the url to be /authentication/sign-in
like this: 到身份验证摘要,以保持url为
/authentication/sign-in
如下所示:
.state('app.authentication', {
abstract: true,
url: '/authentication',
templateUrl: '/modules/users/client/views/authentication/authentication.client.view.html',
controller: 'AuthenticationController',
controllerAs: 'vm'
})
.state('app.authentication.signup', {
url: '/sign-up',
templateUrl: '/modules/users/client/views/authentication/signup.client.view.html',
controller: 'AuthenticationController',
controllerAs: 'vm',
data: {
pageTitle: 'Signup'
}
})
.state('app.authentication.signin', {
url: '/sign-in?err',
templateUrl: '/modules/users/client/views/authentication/signin.client.view.html',
controller: 'AuthenticationController',
controllerAs: 'vm',
data: {
pageTitle: 'Signin'
}
});
So, when through the application I click on the sign in
link it load the main abstract page and the authentication abstract page correctly and it is populated by the sign in page too. 因此,当我通过应用程序单击
sign in
链接时,它会正确加载主摘要页面和身份验证摘要页面,并且也由登录页面填充。
But when I'm in the sign in page and I try to reload the page it fire an error: 但是,当我进入登录页面并尝试重新加载页面时,它会引发错误:
WARNING: Tried to load angular more than once.
Uncaught Error: [ng:btstrpd] App already bootstrapped with this element 'document'
I wish my app to be reloaded in every state and to look as it should. 我希望我的应用程序可以在每个状态下重新加载并保持应有的状态。
What I did wrong here? 我在这里做错了什么?
Here is also my HTML: 这也是我的HTML:
core.client.view.html : core.client.view.html :
<header ng-include="'/modules/core/client/views/partials/header.client.partial.view.html'" ng-controller="HeaderController" id="header" class="topnavbar-wrapper"></header>
<div data-ui-view autoscroll="false" id="wrapper"></div>
<footer ng-include="'/modules/core/client/views/partials/footer.client.partial.view.html'"></footer>
authentication.client.view.html (default): authentication.client.view.html (默认):
<section class="row">
<h3 class="col-md-12 text-center">Sign in using your social accounts</h3>
<div class="col-md-12 text-center">
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/facebook')" ng-src="/modules/users/client/img/buttons/facebook.png"></a></div>
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/twitter')" ng-src="/modules/users/client/img/buttons/twitter.png"></a></div>
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/google')" ng-src="/modules/users/client/img/buttons/google.png"></a></div>
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/linkedin')" ng-src="/modules/users/client/img/buttons/linkedin.png"></a></div>
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/github')" ng-src="/modules/users/client/img/buttons/github.png"></a></div>
<div class="social-account-container social-button"><a class="btn"><img class="img-responsive" ng-click="vm.callOauthProvider('/api/auth/paypal')" ng-src="/modules/users/client/img/buttons/paypal.png"></a></div>
</div>
<div ui-view></div>
</section>
This usually happens when you accidentally use both ng-app
and angular.bootstrap
to bootstrap an application. 当您不小心同时使用
ng-app
和angular.bootstrap
引导应用程序时,通常会发生这种情况。
<html>
...
<body ng-app="myApp">
<script>
angular.bootstrap(document.body, ['myApp']);
</script>
</body>
</html>
Note that for bootstrapping purposes, the <html>
element is the same as document, so the following will also throw an error. 请注意,出于引导目的,
<html>
元素与document相同,因此以下内容也会引发错误。
<html>
...
<script>
angular.bootstrap(document, ['myApp']);
</script>
</html>
You can also get this error if you accidentally load AngularJS
itself more than once. 如果不小心多次加载
AngularJS
本身,也会出现此错误。
<html ng-app>
<head>
<script src="angular.js"></script>
...
</head>
<body>
...
<script src="angular.js"></script>
</body>
</html>
Source: angular documentation 来源: 角度文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.