[英]ng-init function called 1000+ times
I have a module like this : 我有一个像这样的模块 :
angular.module('BlurAdmin.pages.calendar', [])
.config(routeConfig);
/** @ngInject */
function routeConfig($stateProvider, $urlRouterProvider) {
$stateProvider
.state('calendar', {
url: '/calendar',
templateUrl : 'http://localhost:8080/gestionprojet/dashboardCalendar',
title: 'Calendrier'
})
}
And a directive : 和一个指令 :
angular.module('BlurAdmin.pages.dashboard')
.directive('dashboardCalendar', dashboardCalendar);
/** @ngInject */
function dashboardCalendar() {
return {
restrict: 'E',
controller: 'DashboardCalendarCtrl',
templateUrl: 'http://localhost:8080/gestionprojet/dashboardCalendar'
};
}
And a Controller : 还有一个控制器 :
angular.module('BlurAdmin.pages.dashboard')
.controller('DashboardCalendarCtrl', DashboardCalendarCtrl);
/** @ngInject */
function DashboardCalendarCtrl(baConfig, $uibModal, $scope, eventFactory) {
/**
* Get Events
*/
$scope.getEvents = function () {
eventFactory.getEvents()
.success(function (data) {
$scope.userEvents = data;
$scope.userEvents = $scope.renderEventJSonToFullCalendar($scope.userEvents);
})
.error(function (data, status) {
console.log("fail : " + data);
$scope.errorMessage = "Erreur lors du chargement des évènements : " + data + ' ' + status;
})
};
}
And In a partial page I'm init the function in my controller with : 在部分页面中,我使用以下命令初始化控制器中的函数:
<div ng-init="getEvents()">
<dashboard-calendar>
<div id='calendar' class="blurCalendar"></div>
</dashboard-calendar>
</div>
Here the function is getting called a 1000 times and counting even when I'm not loading the directive page... 在这里,该函数被调用了1000次,并且即使我没有加载指令页面也正在计数...
What's the problem here cause I ain't seeing one. 这是什么问题,导致我看不到一个。 Thank you
谢谢
Rather than using ng-init
, you should be using resolve
in your state definitions: 而不是使用
ng-init
,您应该在状态定义中使用resolve
:
$stateProvider
.state('calendar', {
url: '/calendar',
templateUrl : 'http://localhost:8080/gestionprojet/dashboardCalendar',
title: 'Calendrier',
resolve: {
myEvents: function(eventFactory){
return eventFactory.getEvents();
}
}
})
myEvents
will then be available in your controller as a dependency. 然后,
myEvents
将作为依赖项在您的控制器中可用。
问题是我要为状态和指令放置相同的模板
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.