【问题标题】:Angular2 - Uncaught ReferenceError: ng is not definedAngular2 - 未捕获的 ReferenceError:未定义 ng
【发布时间】:2016-01-11 17:26:59
【问题描述】:

我正在尝试运行一个基本的 Angular2 应用程序。但是,当我使用live-servernpm start 运行应用程序时,目前遇到以下错误:

Uncaught ReferenceError: ng is not defined

我安装了最新的 Angular2 npm。这是我的标记:

<html>
    <head>
        <title></title>
        <script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script>
        <script src="https://code.angularjs.org/tools/typescript.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>

        <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
        <script src="app.js"></script>
    </head>
    <body>
        <first-comp></first-comp>
    </body>
</html>

我的 app.js 文件:

var FirstComponent = ng.
    Component({
        selector: 'first-comp'  
    })
    .View({
        template: '<h2>I am learning {{ myFramework }}</h2>'
    })
    .Class({
        constructor: function() {
            this.myFramework = "Angular2";
        }
    });

document.addEventListener('DOMContentLoaded', function() {
    ng.bootstrap(firstComponent);
});

【问题讨论】:

  • 首先你有两次angular2.dev.js,其次如果你想用ES5编写你应该使用umd包之一。检查这个table

标签: javascript angular


【解决方案1】:

发件人:

https://angular.io/guide/quickstart

在您开始使用它之前,您缺少 boot.js 文件,它是引导程序的全部内容,在您的文件中,您在 DOM 加载之前使用了 ng 对象 (DOMContentLoaded)。这就是为什么你有包装:

document.addEventListener('DOMContentLoaded', function() {
    ng.bootstrap(firstComponent);
});

【讨论】:

  • 是的,tuts 课程在这一点上完全没用 :( 我无法继续...尝试 5 分钟安装
  • 大声笑,这绝对不是5 minute 安装,很快就会发布新问题
  • 是的,大声笑...介意看看吗? stackoverflow.com/questions/34730010/…
猜你喜欢
  • 2016-04-22
  • 1970-01-01
  • 2023-01-23
  • 2016-11-03
  • 2011-01-05
  • 2016-01-02
  • 2013-10-06
  • 2016-12-17
相关资源
最近更新 更多