Uladz Kha
Uladz Kha

Reputation: 2354

Why i have two messages after load page in console using AngularJS?

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: enter image description here

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

Answers (2)

miqe
miqe

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

Sajeetharan
Sajeetharan

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

Related Questions