Angularjs 缩小最佳实践 [英] Angularjs minify best practice

查看:22
本文介绍了Angularjs 缩小最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html 和事实证明,如果您缩小 javascript,angularjs 依赖注入会出现问题所以我想知道如果不是

I'm reading http://www.alexrothenberg.com/2013/02/11/the-magic-behind-angularjs-dependency-injection.html and it turned out that angularjs dependency injection has problems if you minify your javascript so I'm wondering if instead of

var MyController = function($scope, $http) {
    $http.get('https://api.github.com/repos/angular/angular.js/commits')
      .then(function(response) {
        $scope.commits = response.data
      })
  }

你应该使用

var MyController = ['$scope', '$http', function($scope, $http) {
  $http.get('https://api.github.com/repos/angular/angular.js/commits')
    .then(function(response) {
      $scope.commits = response.data
    })
}]

总而言之,我认为第二个代码段是针对旧版本的 angularjs 但是....

all in all I thought the second snippet was for the old version of angularjs but ....

我应该一直使用注入方式(第二种)吗?

Should I always use the inject way (the second one) ?

推荐答案

是的,永远!这样,即使您的 minifer 将 $scope 转换为变量 a 并将 $http 转换为变量 b,它们的身份仍然保留在字符串中.

Yes, always! So this way even if your minifer converts $scope to variable a and $http to variable b, their identity is still preserved in the strings.

请参阅本页 AngularJS 文档,向下滚动到关于缩小的说明.

See this page of AngularJS docs, scroll down to A Note on Minification.

更新

或者,您可以在构建过程中使用 ng-annotate npm 包来避免这种冗长.

Alternatively, you can use ng-annotate npm package in your build process to avoid this verbosity.

这篇关于Angularjs 缩小最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆