【问题标题】:Compiling CoffeeScript with AngularJS without Yeoman在没有 Yeoman 的情况下使用 AngularJS 编译 CoffeeScript
【发布时间】:2014-04-15 01:59:37
【问题描述】:

我尝试将 a little app in AngularJS 从 JavaScript 翻译成 CoffeeScript,作为学习 CoffeeScript 的一种练习。

script appcoffee.coffee 现在以

开头
'use strict'

UsrAdmApp = angular.module 'UsrAdmApp', ['ngAnimate', 'ui.bootstrap']

UsrAdmApp.controller 'UsrAdmCtrl', class

    constructor: (@$scope, @$http, @$timeout) ->
        # Init scope
        # etc.

我尝试使用简单的coffee appcoffee.coffee 进行编译,但找不到angular 模块。

ReferenceError: angular is not defined
    at Object.<anonymous> (.../appcoffee.coffee:6:15)
    at Object.<anonymous> (.../appcoffee.coffee:56:4)
    at Module._compile (module.js:456:26)

我尝试了-r angular 选项,但我得到了Error: Cannot find module 'angular'。我尝试将angular.js 文件与咖啡文件放在同一目录中,我也尝试了-r angular.js,让npm install angularjsnode_modules 文件夹放在同一级别,但我没有编译该脚本.. .

目前我不想使用 yeoman 或 grunt,因为我想先了解整个事情在一个小项目上是如何工作的。

【问题讨论】:

    标签: angularjs coffeescript


    【解决方案1】:

    我遇到了同样的问题,并在用户 icktoofay 的另一个帖子上找到了答案:

    选项必须放在文件之前,例如:

     coffee -cw script.coffee
    

    否则,它会尝试立即运行 script.coffee 作为 Node.js 脚本,并将选项 -c 和 -w 传递给它。那不是你想要的;如果你想让 CoffeeScript 编译器获得选项,它必须在文件名之前。

    换句话说,如果没有 '-c' 选项,coffee 会尝试编译和运行脚本,而您的终端不知道 angular 是什么。

    【讨论】:

    • 完美。这可能太明显了,以至于我在任何地方都找不到。非常感谢,这很好用。
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多