简体   繁体   English

具有主视图/详细视图的AngularJs SPA:“慢速”方法

[英]AngularJs SPA with master/detail views: a “slow” approach

My app is a simple AngularJS SPA, which is essentially a contacts manager: the master (landing) page is a list of persons, one per row, in a ng-repeat loop. 我的应用程序是一个简单的AngularJS SPA,本质上是联系人管理器:母版(着陆)页面是ng-repeat循环中每行一个人的列表。
Each person, clicked, shows the details page: a form with many fields. 单击每个人,将显示详细信息页面:具有许多字段的表单。

Currently my router is like this 目前我的路由器是这样的

app.config(function ($routeProvider) {
  $routeProvider
    ...
    .when('/', {
      templateUrl: 'views/persons.html',
      controller: 'PersonsController'
    })
    .when('/details/:personId', {
      templateUrl: 'views/person.html',
      controller: 'PersonsController'
    })
    ...
  });

Ie: one controller for both master ('person s ') and detail ('person') view, and two separate views (of course). 即:一个用于主视图(“ person s ”)和详细信息视图(“ person”)的控制器,以及两个单独的视图(当然)。

The link to the Detail page on each row in the master view does: 主视图中每一行上的“ Detail page的链接执行以下操作:

$location.path('/detail/' + id);

and the Back to master page link in the detail view does: 并且详细视图中的“ Back to master page链接执行以下操作:

$location.path('/');

This approach (which is the "institutional" approach, I suppose...) has many drawbacks: 这种方法(我想是“机构”方法)有很多缺点:

  • since my persons list is quite big (300 persons and growing), with one small image for each person, the loading of the master page is very slow (~5 - ~8 seconds), even if I save the $scope.persons array in a service, to avoid to resync it from server: the slowness is due to Angular, I suppose (the browser freezes for almost the whole time...). 由于我的人员列表很大(300人,并且还在增长),每个人只有一个小图像,因此即使保存$scope.persons数组,母版页的加载也非常慢( $scope.persons秒)。在服务中,为了避免从服务器重新同步它:速度缓慢是由于Angular造成的,我想(浏览器几乎在整个时间内都冻结了……)。 I did not yet try to optimize my code (and reduce the $watch es), I know :-), but... 我还没有尝试优化我的代码(并减少$watch es),我知道:-),但是...

  • When returning to master page, the scroll position is lost: the view is shown from the top, which could be very annoying, for the user... :-( 返回母版页时,滚动位置丢失:该视图从顶部显示,对于用户来说可能非常烦人... :-(

The question is : 问题是

Is there a better approach you'd suggest? 您是否建议采用更好的方法? (for example, just changing the template view, instead to change location...) (例如,仅更改模板视图,而不是更改位置...)

Every {{ .. }} , ng- counts and ng-repeats multiply them, and then automatically add to watchers. 每个{{ .. }}ng-repeats ng-ng-repeats将它们相乘,然后自动添加到观察者中。 So, you need to look for angular-once, bind-once or angular-js 1.3 's one time binding syntax {{::variable}} 因此,您需要查找angular-once,bind-once或angular-js 1.3的一次性绑定语法{{::variable}}

According to your repository, you using angular 1.3, so you can use angular's one time binding syntax ::, so, you can just try to change {{ to {{:: for those you don't really need 2 way binding. 根据您的存储库,您使用angular 1.3,因此可以使用angular的一次性绑定语法::,因此,对于那些实际上不需要2种方式绑定的用户,您可以尝试将{{更改为{{::

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM