简体   繁体   English

角-路由为什么我的控制器两次被调用

[英]Angular - routing why do I getting my controller 2 times called

I am using routing in angular app. 我在Angular应用中使用路由。 while the page loads, I am getting the controller function triggers 2 times.. 页面加载时,我得到了控制器功能触发2次。

How to avoid that or is it have any useful meaning to call 2 times? 如何避免这种情况,或者打电话两次有什么用呢?

any one help me and explain the mistake what i do? 有人可以帮助我并解释我的错误吗?

here is my html: //i use jade! 这是我的html://我用玉!

header
            h1 Header
        div.content(ng-controller="HomeController")
            div(ng-view)
        footer
            h5 Footer

here is my js: 这是我的js:

var locations = angular.module('location', ['ngRoute']);
locations.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'HomeController',
            templateUrl: 'views/home.html'
        })
        .when('/inbox/:name', {
            controller: 'InboxController',
            templateUrl: 'views/inbox.html'
        })
        .otherwise({redirectTo: '/'});
}]);
locations.controller('HomeController', ['$scope', function($scope){
    console.log('hi'); // i am getting 2 times consoled! -why?
   }]);

Because you have div.content(ng-controller="HomeController") in your template. 因为您的模板中包含div.content(ng-controller="HomeController") You don't need to explicitly define ng-controller directive to your header template because It has already been associated using $routeProvider 您不需要为标头模板明确定义ng-controller指令,因为它已经使用$routeProvider进行了关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM