简体   繁体   English

具有指令性能的Angular 1.4组件

[英]Angular 1.4 components with directives performance

Background 背景

We have a old project which is developed with Angular 1.4.8. 我们有一个用Angular 1.4.8开发的旧项目。 For example: 例如:

// controller  
$scope.nameOpts = {
    ...
};
$scope.type = {
    ...
};


// view
<my-form label-width="labelWidth">
    <my-item >
        <my-item-label>
            <span class="form-title-txt fs-txt" ng-bind="i18n.name"></span>
        </my-item-label>
        <div class="clearfix">
            <div class="f-left">
                <input class="w311"
                    ng-style="{width: 2 * i18n.btn_width}"
                    ng-model="nameOpts.name"
                    ng-change="nameOpts.change(nameOpts.name)">
            </div>
        </div>
    </my-item>
    <my-item>
        <my-item-label>
            <span class="form-title-txt cmn-fs-txt" ng-bind="i18n.list_index"></span>
        </my-item-label>
        <div class="f-left" >
            <my-btn-radio-group
                items="type.items"
                selected-id="type.selectedId"
                change="type.change">
            </my-btn-radio-group>
        </div>
    </my-item>
    ...
</my-form>

We want to refactor the project with custom components using directives. 我们希望使用指令使用自定义组件来重构项目。 The following is the code of our component. 以下是我们组件的代码。

// myNameInput component
define([], function () {
    function ctrl($scope) {
        $scope.nameOpts = {...};
        ...
    }

    return {
        templateUrl: 'name.html',
        replace: true,
        scope: {},
        controller: ctrl
    };
});

// name.html
<div>
   <my-item>
        <my-item-label>
            <span class="form-title-txt fs-txt" ng-bind="i18n.name"></span>
        </my-item-label>
        <div class="clearfix">
            <div class="f-left">
                <input class="w311"
                    ng-style="{width: 2 * i18n.btn_width}"
                    ng-model="nameOpts.name"
                    ng-change="nameOpts.change(nameOpts.name)">
            </div>
        </div>
    </my-item>
</div>

We require the component in the controller and use it in the view: 我们需要控制器中的组件,并在视图中使用它:

// controller
define([
    'app/components/insCharSet/charSet'
], function () {...});

// view
<my-form label-width="labelWidth">
    <my-name-input></my-name-input>
    ...
</my-form>

Problem 问题

We can develop components with directives. 我们可以使用指令来开发组件。 But the performance is not good. 但是性能不好。 Compare with the view in the previous way, the components render is slower. 与以前的视图相比,组件的渲染速度较慢。

I have a few questions: 我有几个问题:
1) why the custom components render slower? 1)为什么自定义组件渲染速度较慢?
2) Although the position of component my-name-input is before, actually it is rendered after my-btn-radio-group , why? 2)尽管组件my-name-input位置在前面,但实际上在my-btn-radio-group之后呈现,为什么?
3) Is it a good practice using custom components with Angular 1.4? 3)在Angular 1.4中使用自定义组件是一种好习惯吗?
4) Or what is the best practice? 4)或最佳做法是什么?

Each time you divide your app in slices of components, it will take longer to render. 每次将应用划分为多个组件时,渲染时间将更长。 AngularJS has a bunch of hidden behavior in addition of the watchers (digestion cycles, scopes inheritance etc.). 除了观察者(消化周期,作用域继承等)外,AngularJS还具有许多隐藏行为。

With your new directive, angularjs has to create a new scope, binding it to the virtual DOM etc. The more your create directive components, the more it will render slowly, the more you gain in consistence and maintainability. 使用新指令,angularjs必须创建一个新的作用域,并将其绑定到虚拟DOM等。create指令的组件越多,渲染速度越慢,一致性和可维护性就越多。

The best practice I can advice is to upgrade your app to angular 1.5 at least, to have the real "component" features instead of using directives that are not meant to be used this way. 我可以建议的最佳实践是至少将您的应用程序升级到angular 1.5,以具有真正的“组件”功能,而不要使用不打算以这种方式使用的指令。

If you can't, depending on your app scale, it is a good idea to refactor it into components. 如果不能,则根据您的应用程序规模,将其重构为组件是一个好主意。 You will gain in visibility, maintainability. 您将获得可见性,可维护性。 Even if you have to loose some render time. 即使您必须花费一些渲染时间。

I suppose you did not lose a huge rendering time between the two versions? 我想您在两个版本之间没有浪费大量的渲染时间吗?

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

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