AngularJs: controller is called twice by using $routeProvider

前端 未结 10 2199
慢半拍i
慢半拍i 2020-12-04 14:43

Module routes:

var switchModule = angular.module(\'switchModule\', []);

switchModule.config([\'$routeProvider\', function($routeProvider) {
    $routeProvid         


        
相关标签:
10条回答
  • 2020-12-04 14:47

    Remove the ng-controller directive from your template pages if exists .

    0 讨论(0)
  • 2020-12-04 14:48

    I had face same issue today. I had added controller name in my $routeProvider and also in my html.

    $routeProvider
        .when('/login', {
                controller: 'LoginController',
                templateUrl: 'html/views/log-in.html'
         })
    

    and in my view as

    <div class="personalDetails" ng-controller="LoginController"> </div>
    

    You can remove controller name either from your view or from your routeprovider.

    0 讨论(0)
  • 2020-12-04 14:59

    I had the same problem , and it seems there is a stupid bug with routing. There is some kind of redirection going on.

    to fix it , i just added a slash in the href , like :

    <li><a href="#/build-content/"></a></li>
    

    I hope it will fix things for you too.

    0 讨论(0)
  • 2020-12-04 15:00

    its because you have multiple ui-view

    0 讨论(0)
  • 2020-12-04 15:05

    I had declaration of SomeController for each view partial in a single state. That caused duplicate event firing.

     .state('chat', {
                url: '/',
                views: {
                    'content@': {
                        templateUrl: 'views/chat_wv1.html',
                        controller: 'ChatController'
                    },
                    'form@': {
                        templateUrl: 'views/chat_wv_placeholder.html',
                        controller: 'ViewController'
                    },
                    'sidePanel@': {
                         templateUrl: 'views/sidePanel_wv.html'
                         /* removing this part solved my issue*/
                         /*controller: 'ChatController'*/
                    }
    
                }
            })
    

    Hope this helps some one else.

    0 讨论(0)
  • 2020-12-04 15:06

    A controller can be added to more than one element of the DOM, so this problem can occur if this has been done e.g. :

      <div id="myDiv1" ng-controller="myController" ....></div>
      ....
      <div id="myDiv2" ng-controller="myController" ....></div>
    
    0 讨论(0)
提交回复
热议问题