title: Angularjs - Controller call init() function multiple times
body: <p>I have a problem while I'm making an angularjs app.
  I create a controller and bind it in routeProvider. With a function to initialize values for the controller like this.</p>
  
  <pre><code>angular.module('app.Login', ['app.rpc','ngRoute'])
  .config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/login', {
          templateUrl: '/static/tour/app/login/login.html',
          controller: 'loginController'
      });
  }])
  .controller('loginController', function($scope, $location, rpc) {
      $scope.init = function() {
          $scope.error = "";
          console.log("Login Initialized");
      }
      $scope.init();
  });
  </code></pre>
  
  <p>for login.html like this</p>
  
  <pre><code>&lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-12"&gt;
              &lt;h1 class="page-header"&gt;Login&lt;/h1&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-12"&gt;
              &lt;div class="alert alert-danger alert-dismissable" role="alert" ng-show="error"&gt;
                  &lt;button type="button" class="close" data-dismiss="alert" aria-label="Close"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;/button&gt;
                  &lt;strong&gt;Error: &lt;/strong&gt;
                  &lt;span&gt;{{error}}&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-xs-12"&gt;
              &lt;form&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Username&lt;/label&gt;
                      &lt;input type="text" class="form-control" ng-model="username" placeholder="Username"&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label&gt;Password&lt;/label&gt;
                      &lt;input type="password" class="form-control" ng-model="password" placeholder="Password"&gt;
                  &lt;/div&gt;
                  &lt;button class="btn btn-primary action-btn" ng-click="login()"&gt;Login&lt;/button&gt;
              &lt;/form&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When I run the app, it call init() 3 times like this.
  <a href="http://i.stack.imgur.com/LdCCK.jpg" rel="nofollow">login</a></p>
  
  <p>Find out in this stackoverflow that many people also get this troble. All of them solved by make sure that controller doesn't bind more than one time (in routProvider and in template), and check that only one ng-app in the template.
  My main html like this.</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html ng-app="app"&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Touring&lt;/title&gt;
      &lt;!--Bootstrap--&gt;
      &lt;link rel="stylesheet" href="/static/tour/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="/static/tour/css/bootstrap-theme.min.css"&gt;
      &lt;!--/Bootstrap--&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;ng-view&gt;&lt;ng-view&gt;
      &lt;!--Angular--&gt;
      &lt;script src="/static/tour/js/angular.min.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/js/angular-route.min.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/js/angular-cookies.min.js"&gt;&lt;/script&gt;
      &lt;!--/Angular--&gt;
      &lt;!--App--&gt;
      &lt;script src="/static/tour/app/app.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/app/rpc/app-rpc.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/app/login/app-login.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/app/dashboard/app-dashboard.js"&gt;&lt;/script&gt;
      &lt;script src="/static/tour/app/memo/app-memo.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>For my main app like this.</p>
  
  <pre><code>angular.module('app',
      ['ngCookies',
      'ngRoute',
      'app.Login',
      'app.Dashboard',
      'app.Memo',
  ])
  .config(['$routeProvider', function($routeProvider) {
      $routeProvider.when('/', {
          resolve: {
              "check": function($cookies, $location) {
                  if(!$cookies.get("user_id")) {
                      $location.path("/login");
                  }
                  else {
                      $location.path("/dashboard");
                  }
              }
         }
      })
      .when('/logout', {
          resolve: {
              "check": function($cookies, $location) {
                  if($cookies.get("user_id")){
                      alert("You have been Logout");
                  }
                  $cookies.remove("user_name");
                  $cookies.remove("user_id");
                  $cookies.remove("token");
                  $cookies.remove("dbname");
                  $location.path("/login");
              }
          }
      });
  }]);
  </code></pre>
  
  <p>It ok for this controller, I just want to initialize a text but it's not ok for init in some controller that need some AJAX request to server side, could make too many request for noting. Anyone can solve this please help me. Thanks.</p>
  
  <p>PS1. My practice base on angular-seed <a href="https://github.com/angular/angular-seed" rel="nofollow">https://github.com/angular/angular-seed</a></p>
  
  <p>PS2. At first, I also used jquery and it show WARNING: Tried to load angular more than once. Once I remove jquery it never show warning again, but it didn't fix my problem </p>