AngularJs,创建控制器时有什么好处?(AngularJs, What is better when creating a controller?)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
AngularJs,创建控制器时有什么好处?(AngularJs, What is better when creating a controller?)

在AngularJs中创建控制器时更好的是:

1-

angular.module('myApp') .controller('MyController',['$scope','dependencies','myService', function($scope, dependencies, myService ) { }]);

2-

angular.module('myApp') .controller('MyController', function($scope, dependencies, myService ) { });

What is better when creating a controller in AngularJs:

1-

angular.module('myApp') .controller('MyController',['$scope','dependencies','myService', function($scope, dependencies, myService ) { }]);

2-

angular.module('myApp') .controller('MyController', function($scope, dependencies, myService ) { });

最满意答案

性能方面,第一个在技术上更好,虽然成本可能可以忽略不计。 如果你以第二种方式执行它,angular会在控制器函数上调用.toString()函数,然后解析字符串以确定依赖关系。 因此,您可以自己声明它们,或者angular会将您的函数解析为字符串以确定它们。

作为开发人员,我发现更容易阅读和编写第二个版本。 请注意,如果您计划使用uglifying / minifying代码,则必须将内容更改为第一个表单。 有些工具会自动为你做这个(例如ngAnnotate),这样你就可以用第二种方式编写它,但是让它以第一种方式部署。

Performance wise, the first is technically better, although that cost is probably negligible. If you do it the second way, angular literally calls the .toString() function on your controller function, and then parses the string to determine the dependencies. So you can either declare them yourself, or angular will parse your function as a string to determine them.

As a developer, I find it easier to read and write the second version. Just be aware that you have to change things to the first form if you plan on uglifying/minifying your code. Some tools will do this for you automatically (ngAnnotate, for example), so that you can write it the 2nd way, but have it deployed the first way.

更多推荐

本文发布于:2023-04-17 08:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/798a77db6969c48307938ff0097efa68.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   好处   AngularJs   creating   controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!