加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular JS viewContentLoaded在初始主页载入上加

发布时间:2020-12-17 07:33:16 所属栏目:安全 来源:网络整理
导读:我注意到,在我的初始首页加载中,我的viewContentLoaded正在触发两次,为什么呢? app.run(function ($rootScope,$log) { 'use strict'; $rootScope.$state = $state; $log.warn("gets here"); $rootScope.$on('$viewContentLoaded',function(){ $log.warn("ge
我注意到,在我的初始首页加载中,我的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"

而我的路由如下

$urlRouterProvider.when('','/');

  // For any unmatched url,send to 404
  $urlRouterProvider.otherwise('/404');

  $stateProvider
    .state('home',{
      url: '/',templateUrl: 'views/search.html',controller: 'SearchCtrl'
    })

我的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上的完整源代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读