视图一次又一次地被初始化 [英] View is getting initialized again and again

查看:30
本文介绍了视图一次又一次地被初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个与此类似的仪表板

我的应用代码结构类似于:

index.html:<div ui-view=""></div>主文件<div ui-view=""></div><div ng-include='"app/dashboard/sidebar.html"'></div>

我在 main.html 中完成了视图的嵌套,我在其中注入了不同的视图.另外,由于我的右侧边栏是固定的,我希望它在所有主要视图中都是通用的.所以,我刚刚将它包含在我的 main.html 中.

现在,问题是无论我向下滚动页面还是在侧边栏中执行任何操作,我的 sidebar.html 都会一次又一次地初始化.我已经通过为 sidebar.html 视图中使用的每个控制器功能打印控制台日志来验证它.

这个问题与我的这个post有关:之前,我无法弄清楚实际问题.

以下是我的控制器和玉石代码:

angular.module('myApp').controller('SidebarCtrl', function($scope, $rootScope) {$scope.message = {};$scope.addSideBarToggleClass = function() {console.log("addSideBarToggleClass");返回真;}$scope.getStatusClass = 函数(状态){console.log("getStatusClass");返回在线";}$scope.openChat = 函数(接收者){console.log("openChat");}//等等...});

<aside ng-class="{ 'control-sidebar-open' : addSideBarToggleClass()}"ng-controller="SidebarCtrl"><ul><li ng-class="{active: isTabSelected('chat')}"><a data-toggle="tab" ng-click="updateCurrenTab('chat')"></a><li ng-class="{active: isTabSelected('home')}"><a data-toggle="tab" ng-click="updateCurrenTab('home')"></a><div><div ng-class="{active: isTabSelected('home')}"><h3>最近的活动</h3>

<div ng-class="{active: isTabSelected('chat')}"><div><h4>聊天{{noOfUsersOnline}}</h4><div>朋友<a href="#" ng-repeat="user in users" ng-click="openChat(user)"><span ng-class="getStatusClass(user.status)"></span>{{user.name}}</a>

</一边>

我看到许多 "addSideBarToggleClass""getStatusClass" 的日志,每次我点击 openChat 时,我都会看到 <代码>"openChat" 然后又是 "addSideBarToggleClass""getStatusClass"

谁能指出这种行为可能存在的问题?

解决方案

您需要熟悉 摘要循环.

简而言之,每次摘要循环运行时,所有表达式,例如{{name}}ng-show="isActive && isEnabled", 被 Angular "$watched" 评估(有时不止一次).这意味着,如果您在表达式中调用函数:

$scope.isShown = function(){console.log("希望看到这段文字很多次");返回真;};

该函数将在每个摘要循环中执行.

摘要循环会在 Angular 中的某些内容调用 $scope.$digest 时运行,默认情况下会发生在 ng-clickng-更改$http.then等.

I'm building a dashboard similar to this one Admin Dashboard Theme

I'm implementing the Right Side SideBar as show in the screenshot

My App code structure is something like:

index.html:
<div ui-view=""></div> 

main.html
<div ui-view=""></div> 
<div ng-include='"app/dashboard/sidebar.html"'></div> 

I have done nesting of view in my main.html where I'm injecting different views. Also, since my right side sidebar is fixed, I want it to be common in all the main views. So, I have just included it in my main.html.

Now, the problem is somehow my sidebar.html is getting initialized again and again no matter if I scroll my page down or perform any action inside sidebar. I have verified it by printing console logs for every controller function which are used in sidebar.html view.

This problem is related to my this post: Earlier, I wasn't able to figure out the actual issue.

Following is my controller and jade code:

angular.module('myApp')
  .controller('SidebarCtrl', function($scope, $rootScope) {
    $scope.message = {};

    $scope.addSideBarToggleClass = function() {
      console.log("addSideBarToggleClass");
      return true;
    }

    $scope.getStatusClass = function(status) {
      console.log("getStatusClass");
      return 'is-online';
    }

    $scope.openChat = function(receiver) {
      console.log("openChat");
    }

    // etc...

  });

<aside ng-class="{ 'control-sidebar-open' : addSideBarToggleClass()}" 
       ng-controller="SidebarCtrl">
  <ul>
    <li ng-class="{active: isTabSelected('chat')}">
      <a data-toggle="tab" ng-click="updateCurrenTab('chat')"></a>
    </li>
    <li ng-class="{active: isTabSelected('home')}">
      <a data-toggle="tab" ng-click="updateCurrenTab('home')"></a>
    </li>
  </ul>
  <div>
    <div ng-class="{active: isTabSelected('home')}">
      <h3>Recent Activity</h3>
    </div>
    <div ng-class="{active: isTabSelected('chat')}">
      <div>
        <h4>Chat {{noOfUsersOnline}}</h4>
        <div>Friends
          <a href="#" ng-repeat="user in users" ng-click="openChat(user)"> 
           <span ng-class="getStatusClass(user.status)"></span>
           {{user.name}}</a>
        </div>
      </div>
    </div>
  </div>
</aside>

I see many logs of "addSideBarToggleClass", "getStatusClass" and every time I click on openChat, I see a log of "openChat" and then again "addSideBarToggleClass" and "getStatusClass"

Can anyone please point out what can be the possible problem for this behavior?

解决方案

You need to familiarize yourself with the concept of a digest loop in Angular.

In short, every time a digest loop runs, all expressions, e.g. {{name}} or ng-show="isActive && isEnabled", that are being "$watched" by Angular are evaluated (sometimes more than once). This means that if you are invoking a function inside an expression:

<div ng-show="isShown()">

$scope.isShown = function(){
   console.log("expect to see this text a lot of times");
   return true;
};

the function will be executed on every digest loop.

A digest loop runs any time that something in Angular calls $scope.$digest, which by default happens on things like ng-click or ng-change or $http.then, etc..

这篇关于视图一次又一次地被初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆