我在角应用程序中使用路由。当页面加载时,我得到控制器功能触发2次..
如何避免这种情况或者有两次调用是否有用?
任何人帮助我并解释我的错误?
这是我的html://我用jade!
header
h1 Header
div.content(ng-controller="HomeController")
div(ng-view)
footer
h5 Footer
这是我的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?
}]);
答案 0 :(得分:5)
因为您的模板中有div.content(ng-controller="HomeController")
。您不需要为标头模板明确定义ng-controller
指令,因为它已使用$routeProvider