简体   繁体   English

Angular Controller被调用两次

[英]Angular Controller called twice

I've tried using both the DOM and the stateProvider methods. 我试过同时使用DOM和stateProvider方法。

Feed.html Feed.html

    <div class="container-fluid">
        <div class="panel panel-card" ng-repeat="post in posts" class="col-lg-12 fadeInUp animated" style="-webkit-animation-delay: {{$index * 0.1}}s; animation-delay: {{$index * 0.1}}s;">
            <div class="panel-body bg-primary">{{post.user_id}} from {{post.squad_id}}</div>
            <div class="clearfix"></div>
        </div>
    </div>

feed.controller.js feed.controller.js

(function() {
    'use strict';

    angular
        .module('app')
        .controller('FeedController', FeedController);

    FeedController.$inject = ['$scope'];
    function FeedController($scope) {
        console.log($scope);
    }

})();

routes.config.js route.config.js

(function() {
    'use strict';

    angular
        .module('app')
        .config(routesConfig);

    routesConfig.$inject = ['$locationProvider', '$stateProvider', '$urlRouterProvider', 'RouteProvider'];
    function routesConfig($locationProvider, $stateProvider, $urlRouterProvider, Route) {

      // use the HTML5 History API
      $locationProvider.html5Mode(false);

      // Default route
      //$urlRouterProvider.otherwise('/feed');

      // Application Routes States
      $stateProvider
        .state('app', {
          url: '',
          abstract: true,
          templateUrl: Route.base('app.html'),
          resolve: {
            _assets: Route.require('icons', 'toaster', 'animate')
          }
        })
        .state('app.feed', {
          url: '/feed/',
          templateUrl: Route.base('feed.html'),
          controller: "FeedController",
          resolve: {}
        })
    }

})();

The other way was to insert ng-controller="FeedController" into the top div in feed.html and comment out controller: "FeedController", from routes.config.js 另一种方法是插入ng-controller="FeedController"成顶格feed.html和注释掉controller: "FeedController",routes.config.js

app.html app.html

<!-- top navbar-->
<header ng-include="'templates/top-navbar.html'" ng-class="app.theme.topbar"></header>
<!-- Sidebar-->
<aside ng-include="'templates/sidebar.html'" ng-class="app.theme.sidebar"></aside>
<!-- Main-->
<section>
   <!-- Content-->
   <div ui-view="" autoscroll="false" ng-class="app.views.animation" class="app"></div>
</section>
<!-- Page footer-->
<footer ng-include="'templates/footer.html'"></footer>

Both of these methods console.log($scope) twice with different $id's. 这两种方法console.log($ scope)都使用不同的$ id两次。 This happens on page reload and state change. 这会在页面重新加载和状态更改时发生。

Anyone have any idea's? 有人有想法么?

I'm not where I can test this, but you might try just commenting out the .controller('FeedController', FeedController); 我不是可以对此进行测试的地方,但是您可以尝试注释掉.controller('FeedController', FeedController); line in feed.controller.js . feed.controller.js行。

I think hitting the route will fire off the controller, so you don't need to fire it off there. 我认为击中路线将触发控制器,因此您无需将其触发。

There is no reason to this occur. 没有理由发生这种情况。 But, i suspect url sub state. 但是,我怀疑网址子状态。 Try removing the last bar. 尝试删除最后一个小节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM