简体   繁体   English

角度和普通javascript函数,最佳做法

[英]angular and plain javascript functions, best practice

Is there any difference (maybe in performance) between this style of adding functions to (angular) scripts, or are they essentially equivalent: 这种向(角度)脚本添加函数的样式之间是否有任何区别(也许在性能上),或者它们本质上是等效的:

Option 1: function inside controllers 选项1:控制器内部的功能

AngularApp.component('component', {
    templateUrl: '/domain/app/component.html'
    , controller: function ($scope,$rootScope,api) {

        $scope.var = false;
        getBackendData();

        //get data about available io_engines from the backend
        function getBackendData() {
            console.log("loading backend data...");            
            api.get().then(
                function (response) {                   
                    console.log("Backend data loaded.");
                })
                .catch(function (err) {
                    console.log("Error getting data from backend");
                    console.log(err);
                });
        }
   }

});

Option 2: function outside the controller 选项2:控制器外部功能

 AngularApp.component('component', {
        templateUrl: '/domain/app/component.html'
        , controller: function ($scope,$rootScope,api) {

            $scope.var = false;
            getBackendData();
       }
    });


    //get data about available io_engines from the backend
    function getBackendData() {
                console.log("loading backend data...");            
                api.get().then(
                    function (response) {                   
                        console.log("Backend data loaded.");
                    })
                    .catch(function (err) {
                        console.log("Error getting data from backend");
                        console.log(err);
                    });
     }

I (think to) understand getBackendData() in the second option becomes a global function, but I am not very clear about implications. 我(想)理解第二个选项中的getBackendData()成为全局函数,但是我对含义并不十分清楚。

If you define a function in a component, you will have one function definition per component instance, so in theory it will take more memory. 如果在组件中定义一个函数,则每个组件实例将具有一个函数定义,因此理论上它将占用更多内存。

In the second example, you will only have one function per application. 在第二个示例中,每个应用程序仅具有一个功能。

But such difference is really academic. 但是这种差异确实是学术上的。 The bigger issue here is that these kind of functions should be defined in (or as) a service , so they can be: 这里更大的问题是,这类功能应在服务中定义(或定义为服务) ,因此可以是:

  • tested 测试
  • possibly reused in other places 可能在其他地方重复使用
  • mocked in the component tests 在组件测试中模拟

The Option 1 is better because you are not polluting the global scope. 选项1更好,因为您没有污染全局范围。 Let say that is not a good practice. 可以说这不是一个好习惯。 I suggest you to follow a style guide like this by Todd Motto: 我建议您遵循Todd Motto这样的样式指南:

https://github.com/toddmotto/angular-styleguide/tree/angular-old-es5 https://github.com/toddmotto/angular-styleguide/tree/angular-old-es5

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

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