Mvc4 捆绑、缩小和 AngularJS 服务 [英] Mvc4 bundling, minification and AngularJS services

查看:22
本文介绍了Mvc4 捆绑、缩小和 AngularJS 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法自定义 Asp.Net MVC4 捆绑和缩小功能缩小 js 文件的方式?

Is there a way to customize the way Asp.Net MVC4 bundling&minification feature minifies the js files?

意思是,我不想完全关闭缩小,但按原样"只会破坏 AngularJs.

Meaning, I don't want to completely turn off minification, but "as is" it just breaks AngularJs.

由于 AngularJs 使用 DI 和 IoC 方法在控制器中注入服务,因此:

Since AngularJs uses DI and IoC approach for injecting services in controllers, the following:

function MyController($scope) { }

一旦缩小,就变成:

function MyController(n) { }

通常这不会成为问题,但 AngularJs 使用参数名称来了解要注入的服务.所以 $scope 应该保持 $scope 以及角度控制器中的任何其他参数.其他一切,如局部变量等,都应该正常缩小.

Normally that wouldn't be a problem, but AngularJs uses the parameter names to understand which service to inject. So $scope should remain $scope, as well as any other parameter in angular controllers. Everything else, like local variables, etc, should be minified normally.

我找不到任何关于如何配置 Mvc4 缩小的明确文档,而且全有或全无"似乎相当愚蠢,所以我想我错过了一些东西.

I can't find any clear documentation on how to configure Mvc4 minification, and it seems rather dumb for it to be "all or nothing" so I think I'm missing something.

谢谢.

推荐答案

实际上,您可以(并且应该!)编写 AngularJS 代码,使其缩小安全".http://docs.angularjs.org/guide/di 的依赖注释"部分描述了详细信息 但简而言之,对于全局定义的控制器,您可以编写:

Actually you can (and should!) write AngularJS code so it is "minification safe". Details are described in the "Dependency Annotation" section of http://docs.angularjs.org/guide/di but in short, for globally defined controllers you can write:

MyController.$inject = ['$scope'];

请注意,全局定义的控制器会污染全局命名空间(有关详细信息,请参阅),应避免使用.如果你在模块级别声明一个控制器,你也可以使它的缩小安全:

Please note that globally defined controllers are polluting global namespace (see this for more details) and should be avoided. If you declare a controller on a module level you can make it minification-safe as well:

angular.module('mymodule', []).controller('MyController', ['$scope', function($scope){
//controller code goes here
}]);

这篇关于Mvc4 捆绑、缩小和 AngularJS 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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