如何在AngularJS使用$控制器时,以消除微小错误 [英] How to eliminate minification errors when using $controller in AngularJS

查看:123
本文介绍了如何在AngularJS使用$控制器时,以消除微小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

angular.module('mainApp').
  controller('dynamicRouteController', ['$scope', '$controller', '$routeParams', function($scope, $controller, $routeParams) {
    if(/^\d+$/.test($routeParams.pageOrName)) {
      $scope.controller = $controller('thisController', { $scope: $scope }).constructor;
      $scope.templateUrl = '/www/thisPage';
    } else {
      $scope.controller = $controller('thatController', { $scope: $scope }).constructor;
      $scope.templateUrl = '/www/thatPage';
    }
  }]);

这minifies为:

this minifies to:

"use strict";
angular.module("mainApp").
controller("dynamicRouteController",["$scope",‌​"$controller","$routeParams",function(a,b,c){
/^\d+$/.test(c.pageOrName)?
(a.contro‌​ller=b("thisController",{$scope:a}).constructor,a.templateUrl="/www/thisPage"):
(a‌​.controller=b("thatController",{$scope:a}).constructor,a.templateUrl="/www/thatPa‌​ge")
}])

这是有问题缩小文件,我想因为它{$范围:$范围}被改变我遇到了这个......第一次/使用这种方法。任何人都知道一个更好的方式来写这篇文章,因此正确minifies?

This is having issues minifying, I think its because of the {$scope : $scope} being altered... First time I have run into this/used this method. Anyone know a better way to write this so it minifies correctly?

编辑:所以发生了什么,是它传递{$范围:一}这是好的,但引用的控制器上,当其缩小的,即$范畴,成为a或b或电子邮件视...所以如果我写了codepre-精缩,意思是我硬是查找字母重新presents $范围,另一个控制器,我可以得到它的工作,但多数民众赞成这样哈克!同样,任何想法?

so whats happening, is that it is passing the {$scope: a} which is fine, but on that referenced controller, when its minified, that $scope has become a or b or e depending... so if I write the code "pre-minified", meaning I literally find what letter represents $scope in the other controller, I can get it to work, but thats so hacky! Again, any ideas?

使用咕噜为微小
角1.0.5 ...也许固定在以后的版本?

Using Grunt for minification Angular 1.0.5 ... maybe fixed in later versions?

2日编辑:一个体面的答案是两个控制器扔进同一个文件,明确......这是丑陋的...但它的工作原理!因此,在一个控制器,我声明2分控制器,这是瘸子。如果你知道的另一种方式,请在课堂上分享!

2nd A decent answer is to throw both controllers into the same file, explicitly... which is ugly... but it works! so with in one controller, I am declaring 2 sub controllers, which is lame. If you know of another way please share with the class!

推荐答案

我正在就此问题与@mclenithan,我们想出了什么:

I was working on this issue with @mclenithan and what we came up with is:

$scope.controller = ['$scope', 'service1', 'service2', 
    $controller('thisController', { $scope: $scope }).constructor];

的主要问题是控制器 thisController thatController 有更多的参数,不仅仅是 $范围(在这个例子中,预计服务1 服务2 )。

The main issue was the controllers thisController and thatController had more parameters to inject than just $scope (in this example it expects service1 and service2).

$控制器(...)。构造在返回与重命名为A,B,C,D等。当角是参数缩小的控制器功能试图实例控制器它有问题,试图找到正确的服务注入。

The $controller(...).constructor was returning the minified controller function with the parameters renamed to a, b, c, d, etc. When Angular was trying to instantiate the controller it had issues trying to find the correct services to inject.

使用数组符号而不是仅仅控制器功能的固定问题。请参见在本教程微小记获取更多信息。

Using the array notation instead of just the controller function fixed the issue. See a note on minification in the tutorial for more info.

另请参阅<一个href=\"http://stackoverflow.com/questions/18131834/angularjs-regex-route-for-similar-url-to-load-different-controller-and-view\">this问题的上下文是什么,我们试图做的开始。

Also see this question for context on what we were trying to do to begin with.

这篇关于如何在AngularJS使用$控制器时,以消除微小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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