angularjs app在代码连接后无法正常工作

时间:2016-05-12 10:28:33

标签: javascript angularjs dependency-injection angularjs-controller

我有和angularjs app.I想要将所有 JS 文件连接到一个文件。我正在尝试使用Grunt.js来设置连接JavaScript文件的自动构建过程。

然而,我的应用程序在连接之前运行时没有任何错误。但在合并文件之后我的应用程序抛出错误:$ injector:modulerr 模块错误

以下是缩小后的代码

{-# LANGUAGE RecursiveDo #-}

import Prelude hiding (lookup)
import Data.Map.Lazy (Map, empty, lookup, insert)
import Data.List (transpose)

import Control.Monad.State.Lazy (StateT(..))
import System.Mem.Weak
import System.Environment

type TreeCache = Map Integer (Weak NTree)

data Tree a = Tree a [Tree a]
type Node = (Integer, [Integer])
type NTree = Tree Node

getNode (Tree a _) = a
getVals = snd . getNode

makeTree :: Integer -> IO NTree
makeTree n = fst <$> runStateT (makeCachedTree n) empty

makeCachedTree :: Integer -> StateT TreeCache IO NTree
makeCachedTree n = StateT $ \s -> case lookup n s of
  Nothing -> runStateT (makeNewTree n) s -- makeNewTree n s                                                                                                                                   
  Just wt -> deRefWeak wt >>= \mt -> case mt of
    Nothing -> runStateT (makeNewTree n) s
    Just t -> return (t,s)

makeNewTree :: Integer -> StateT TreeCache IO NTree
makeNewTree n = StateT $ \s -> mdo
  wt <- mkWeak n t Nothing
  (ts, s') <- runStateT
              (mapM makeCachedTree $ children n)
              (insert n wt s)
  let t = Tree (n, values n $ map getVals ts) ts
  return (t, s')

children n = let bf = 10 in let hit = 2 in [bf*n..bf*n+bf+hit-1]

values n [] = repeat n
values n nss = n:maximum (transpose nss)

main = do
  args <- getArgs
  let n = read $ head args in
    do t <- makeTree n
       if length args == 1 then putStr $ show $ take (fromInteger n) $ getVals t else putStr "One argument only!!!"

任何帮助表示赞赏。谢谢:)

4 个答案:

答案 0 :(得分:4)

在缩小js文件时,您需要严格遵循DI的$inject Property Annotationapp.config(function ($routeProvider) { 'use strict'; //code });

app.config(['$routeProvider', function ($routeProvider) {
    'use strict';
    //code as is
}]);

应更改为以下代码。

Inline Array Annotation

var config = function($routeProvider) {
   'use strict';
   //code as is
}
config.$inject = ['$routeProvider'];
app.config(config);

$inject Property Annotation

{
    "cmd" : ["/usr/lib/mono/4.0/xbuild.exe", "$file","/p:Configuration=Release /p:DebugSymbols=false /p:PreBuildEvent=;PostBuildEvent="],
    "info" : "Started $project_path$file_name",
    "env" : {},
    "selector" : "source.csproj"
}

答案 1 :(得分:0)

似乎配置行是您的问题,请尝试app.config(['$routeProvider', function ($routeProvider) { ... }]);

答案 2 :(得分:0)

是的,您的注释不正确。它应该是

app.config(['$routeProvider', function ($routeProvider) {
    'use strict';
    //code
}]);

使用ng-annotate作为安全,简洁的解决方案。

它的作用是当你grunt时它会以正确的方式注释你的定义,所以你不必处理Angular古怪的方式。

上运行ng-annotate
app.config(function($routeProvider) {
  'use strict';
  //code 
});

它将自动完成缩小:

app.config(["routeProvider", function($routeProvider) {
  'use strict';
  //code 
}]);

答案 3 :(得分:0)

您还可以在构建工作流程中添加另一个步骤 - grunt-ng-annotate在这种情况下,您将能够跳过几乎所有严格的DI注释。使用此插件,您可以简化服务/控制器定义,例如:

Wizard

当您将一长串服务注入控制器或其他服务时,它非常有用。您不必记住将每个依赖项放两次。