Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

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();
});

for login.html like this

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <h1 class="page-header">Login</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <div class="alert alert-danger alert-dismissable" role="alert" ng-show="error">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <strong>Error: </strong>
                <span>{{error}}</span>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12">
            <form>
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" class="form-control" ng-model="username" placeholder="Username">
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" class="form-control" ng-model="password" placeholder="Password">
                </div>
                <button class="btn btn-primary action-btn" ng-click="login()">Login</button>
            </form>
        </div>
    </div>
</div>

When I run the app, it call init() 3 times like this. login

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.

<!DOCTYPE html>
<html ng-app="app">
<head>
    <meta charset="UTF-8">
    <title>Touring</title>
    <!--Bootstrap-->
    <link rel="stylesheet" href="/static/tour/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/tour/css/bootstrap-theme.min.css">
    <!--/Bootstrap-->
</head>
<body>
    <ng-view><ng-view>
    <!--Angular-->
    <script src="/static/tour/js/angular.min.js"></script>
    <script src="/static/tour/js/angular-route.min.js"></script>
    <script src="/static/tour/js/angular-cookies.min.js"></script>
    <!--/Angular-->
    <!--App-->
    <script src="/static/tour/app/app.js"></script>
    <script src="/static/tour/app/rpc/app-rpc.js"></script>
    <script src="/static/tour/app/login/app-login.js"></script>
    <script src="/static/tour/app/dashboard/app-dashboard.js"></script>
    <script src="/static/tour/app/memo/app-memo.js"></script>
</body>

</html>

For my main app like this.

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");
            }
        }
    });
}]);

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.

PS1. My practice base on angular-seed https://github.com/angular/angular-seed

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

share|improve this question
    
Move $scope.init(); to <div class="container" ng-init="init();"> put this in page of login – Emir Marques Oct 27 at 10:56
up vote 0 down vote accepted

I already solve it

<ng-view><ng-view>

use this instead

<div ng-view></div>
share|improve this answer
    
Thanks, this save my day !!! :) – Jetarin Chokchaipermpoonphol Oct 27 at 16:11

Concerning your multiple method call, are you sure your current URL is exactly the same as defined in the $routeProvider config? (maybe ngRoute redirects from /login/ to /login and calls your controller twice)

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

AngularJS already contains a lite version of jQuery: jqLite. If jQuery is loaded before Angular, this last one will use it. If not, it use the built-in lite version.

In your case, the error is probably due to a jQuery loading after the Angular loading. Angular already loads its version and you load it again.

Try to load jQuery before Angular or do not load it at all.

share|improve this answer
    
Hello Paul, Thankyou for answer. I tried to put console long in routeProvider resolve. $routeProvider.when('/memo', { resolve: { "check": function($cookies, $location) { console.log("GO TO MEMO"); if(!$cookies.get("user_id")) { $location.path("/login"); } } }, controller: 'memoController', templateUrl: '/static/tour/app/memo/memo.html', }); When I go to memo it show "GO TO MEMO" 1 time , but still init for 3 times. – Jetarin Chokchaipermpoonphol Oct 27 at 12:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.