【发布时间】:2016-08-14 08:21:59
【问题描述】:
我正在使用 angular 和 angular 材料编写企业应用程序,但对中型(在我看来)形式的性能存在问题。尤其是在 IE 中。
(工作演示,见https://codepen.io/tkarls/pen/vGrqWv。点击卡片标题,它在打开前会稍微暂停。尤其是使用IE和移动设备。桌面版chrome效果很好。)
表单中最严重的违规者似乎是一些带有 ng-repeat 的 md-selects。
<md-select ng-model="form.subchannelId" ng-disabled="vm.readOnly">
<md-option ng-repeat="id in subchannelIds" value="{{::id}}">{{::id}}</md-option>
</md-select>
<md-select ng-model="form.serviceReference" ng-disabled="vm.readOnly">
<md-option ng-repeat="id in serviceReferences" value="{{::id}}">{{::countryId}}{{::id}}</md-option>
</md-select>
<md-select ng-model="form.audioCodec" ng-disabled="vm.readOnly">
<md-option ng-repeat="audioCodec in audioCodecs | orderBy:'toString()'" value="{{audioCodec}}">{{::systemVariables.encoders.aac[audioCodec].displayName}}</md-option>
</md-select>
<md-select ng-model="form.audioSource" ng-disabled="vm.readOnly">
<md-option ng-repeat="audioSource in audioSources | orderBy:'toString()'" value="{{audioSource}}">{{audioSource}}</md-option>
</md-select>
<md-select ng-model="form.padSource" ng-disabled="vm.readOnly">
<md-option ng-repeat="padSource in padSources | orderBy:'toString()'" value="{{::padSource}}">{{::padSource}}</md-option>
</md-select>
<md-select ng-model="form.lang" ng-disabled="!form.generateStaticPty || vm.readOnly">
<md-option ng-repeat="langKey in langKeys | orderBy:'toString()'" value="{{::langs[langKey]}}">{{::langKey}}</md-option>
</md-select>
<md-select ng-model="form.pty" ng-disabled="!form.generateStaticPty || vm.readOnly">
<md-option ng-repeat="ptyKey in ptyKeys | orderBy:'toString()'" value="{{::ptys[ptyKey]}}">{{::ptyKey}}</md-option>
</md-select>
数据模型如下:
$scope.subchannelIds = [0, 1, 2]; //up to 63 in real life
$scope.serviceReferences = ["000", "001", "002"]; //up to 999 in real life
$scope.ptys = {
"No programme type": 0,
"News": 1,
"Current Affairs": 2}; //Up to ~30 in real life
$scope.ptyKeys = Object.keys($scope.ptys);
$scope.langs = {
"Unknown": "00",
"Albanian": "01",
"Breton": "02"}; //Up to ~100 in real life
$scope.langKeys = Object.keys($scope.langs);
其他 ng-repeats 很小,每个 3-5 个项目。我认为现代浏览器应该处理这种大小的数据集并非常快速地呈现它。所以希望我对我的 HTML 代码做错了什么。数据是从现实生活中的服务器获取的,但我会预先获取它,因此一旦准备好显示表单,它就已经在 $scope 中了。
在使用普通 js 循环获取数据后,我尝试预生成 HTML。然后只插入 html sn-p,如: {{::preGeneratedHtmlHere}}
但是 Angular 不会将其视为 html 而是文本...
感谢任何有关如何优化它的帮助!
【问题讨论】:
-
如果您可以使用预生成的 html,请尝试绑定 html,例如 ng-bind-html="
"。确保你也有 ng-sanitize -
@Murwa 我尝试将 html 与 ng-bind-html 绑定,但它不起作用。 HTML 包含需要通过 angular 编译才能显示的自定义指令(嗯,角度材料指令)。那么,知道如何将编译后的 HTML 放入 dom 中吗?
-
这会很棘手,因为你需要 $compile 它
-
我发现了这个:stackoverflow.com/questions/17417607/… 并且它有效。我会改变所有的重复,然后看看它对性能做了什么
-
只是让您知道。它确实可以自己进行编译。但它并没有提高性能。我想它仍然是需要插入到 dom 中的相同数量的节点。
标签: angularjs angularjs-ng-repeat angular-material