Reputation: 2354
I'm creating a AngularJS Application, and in JS-Controlleri at the top i wrote the console.log() message:
var myApp = angular.module("myApp", ['ngRoute']);
myApp.controller("defaultController", function ($scope, $location, logicOfMyApp) {
console.log("My daddy is duck, my mommy is duck and i'm duck too ...");
});
and after load the page i has two messages:
Why i have two messages ?
P.S.I cleared console before loading the page.
Routing Script:
myApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/', {
templateUrl: "Partials/Books.html",
controller: "defaultController"
})
.when('/books', {
templateUrl: "Partials/Books.html",
controller: "defaultController"
})
.when('/albums', {
templateUrl: "Partials/Albums.html",
controller: "defaultController"
})
.when('/audios', {
templateUrl: "Partials/Audios.html",
controller: "defaultController"
})
.when('/videos', {
templateUrl: "Partials/Videos.html",
controller: "defaultController"
})
.when('/album/:albumId', {
templateUrl: "Partials/PhotosOfAlbum.html",
controller: "defaultController"
})
;
$routeProvider.otherwise({ redirectTo: '/' });
}]);
Upvotes: 0
Views: 49
Reputation: 3369
you are using defaultController
for /
and /videos
so your controller initialize two times so change the controller for either of the routes and you wont see the log two times
Upvotes: 1
Reputation: 222522
Reasons:
First one is when you load the application it goes through all the controller code.
Second one is since you are using ngRoute when the page gets routed to the specific controller it gets printed.
In Order to avoid, you need to remove ng-controller="defaultController"
from your view if you have mentioned in the config.
Upvotes: 1