我注意到,在我的初始首页加载中,我的viewContentLoaded正在触发两次,为什么呢?
- app.run(function
- ($rootScope,$log) {
- 'use strict';
- $rootScope.$state = $state;
- $log.warn("gets here");
- $rootScope.$on('$viewContentLoaded',function(){
- $log.warn("gets here2");
- });
- });
- "gets here"
- "gets here2"
- "gets here2"
而我的路由如下
我的Index.html
- <div id="canvas">
- <ng-include src="'views/partials/header.html'"></ng-include>
- <!-- container -->
- <div ui-view ></div>
- <!-- /container -->
- <ng-include src="'views/partials/footer.html'"></ng-include>
- </div>
它只是uiView指令实现的细节.它在初始化(链接)阶段触发$viewContentLoaded事件.在这一点上,还没有一个国家知道,但事件仍然被解雇.
然后,一旦$状态服务实际解决您的本地状态,事件再次被触发,转换到它,加载模板等.
所以总结一下,它不是因为错误的配置,也不是您的模板加载两次.这只是uiView的工作原理.
您甚至可以通过完全注释掉您的状态定义和大部分index.html内容(ui-view本身除外)来进行验证. $viewContentLoaded仍将被触发一次.
源代码摘录:
uiView指令声明:
- {
- restrict: 'ECA',terminal: true,priority: 400,transclude: 'element',compile: function (tElement,tAttrs,$transclude) {
- return function (scope,$element,attrs) {
- ...
- updateView(true);
- ...
- }
- }
- }
和updateView函数
- function updateView(firstTime) {
- ...
- currentScope.$emit('$viewContentLoaded');
- currentScope.$eval(onloadExp);
- }
有关更多信息,请参阅GitHub – viewDirective.js上的完整源代码