2

Here is my route config, I am using routeProvider to bind controller to view and not declaring ng-controller in my view still my controller loading twice, I searched for lot of solutions and tried every thing but no use.

 $routeProvider.when("/home", {
    controller: "homeController",
    templateUrl: "app/views/home.html"
}).when("/login", {
    controller: "loginController",
    templateUrl: "app/views/login.html"
}).when("/regcars", {
    controller: "RegCarsController",
    templateUrl: "app/views/client/RegCars.html"
}).otherwise({ redirectTo: "/home/" });

Here is template(view)

  <div class="col-md-6 box box-success  pull-left">
    <div class="box-header with-border">
        <h3 class="box-title">My cars</h3>
        <div class="box-tools pull-right">
            <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
        </div>
    </div>
    <div class="box-body">
        {{CarName}}
    </div>
</div>

And here is my controller

app.controller('RegCarsController', function ($scope) {
  $scope.CarName = "MyCar";
  alert('MyCar');
});

In my above code showing alert twice. Below I have link to call the view, tried with and with out slash at end of href link

 <a href="#/regcars/">
    <i class="fa fa-car fa-2x"></i> <span>My Cars</span>
 </a>
3
  • 2
    are you using ng-controller tag on the top of the element Commented May 5, 2016 at 8:27
  • 3
    please add a JSFiddle Commented May 5, 2016 at 8:35
  • Are you completety sure you don't have an ng-controller attribute in your template, before the markup you are posting here? Commented May 5, 2016 at 8:46

1 Answer 1

0

Some proof of concept that controller is called many times. Strange.

On the other hand - the same code on JSFiddle - shows that controller is executed / fired only once.

angular.module('app', ['ngRoute']).config(function($routeProvider) {
    $routeProvider.when("/home", {
      controller: "homeController",
      templateUrl: "app/views/home.html"
    }).when("/regcars", {
      controller: "RegCarsController",
      templateUrl: "app/views/client/RegCars.html"
    }).otherwise({ redirectTo: "/home" });
  })
  .run(function($templateCache) {
    $templateCache.put('app/views/home.html', '<div>home tempalte</div>');
    $templateCache.put('app/views/client/RegCars.html', '<div>cars template, car name: {{ CarName }}</div>');
  })
  .controller('homeController', function() {})
  .controller('RegCarsController', function($scope) {
    $scope.CarName = "MyCar";
    console.log('Called many times')
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-route.min.js"></script>

<div ng-app='app'>
  <ul><li><a href='#/'>Home</a></li><li><a href='#/regcars'>Cars</a></li></ul>
  <ng-view></ng-view>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Runs fine for me, "Called many times" only appears once in the console.
@C14L For me - about 134 times - till console crash ; / Probably it is tight coupled with origin policy for stackoveflow domain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.