简体   繁体   English

Angular抽象状态无法在页面重新加载上运行:“应用程序已使用此元素自举”

[英]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-appangular.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.

相关问题 ng:btstrpd err 应用程序已经使用此元素进行引导? - ng:btstrpd err App Already Bootstrapped with this Element? Angularjs量角器E2E测试&#39;应用程序已经使用此元素进行自举 - Angularjs Protractor E2E test 'App Already Bootstrapped with this Element' AngularJS:如何调试“已经使用此元素自举引导的应用程序”错误 - AngularJS: How to debug the 'App Already Bootstrapped with this Element' error Angular应用没有启动? - Angular app not being bootstrapped? 如何在反应应用程序中重新加载页面(状态) - How to reload a page (state) in a react app Angular 2路由无法在页面刷新或使用gulp重新加载? - Angular 2 routing not working on page refresh or reload with gulp? 使用downgradeComponent时,无法加载Bootstrapped Angular 2 + Angular 1 Hybrid App - Bootstrapped Angular 2 + Angular 1 Hybrid App not loading when using downgradeComponent Angular $state.go {reload: true} 不应该重新初始化抽象父控制器 - Angular $state.go {reload: true} shouldn't reinitialize the abstract parent controller Angular JS - UI路由器页面重新加载不会设置状态 - Angular JS - UI router page reload won't set the state 在 Angular 8 中使用 window.history.state 导致页面重新加载错误 - Using window.history.state Causing Error On Page Reload In Angular 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM