javascript - Angular2 - Uncaught ReferenceError: ng is not defined - Stack Overflow

I'm trying to run a basic Angular2 app. However currently running into the following error when I

I'm trying to run a basic Angular2 app. However currently running into the following error when I run the app with live-server and npm start:

Uncaught ReferenceError: ng is not defined

I have the latest Angular2 npm installed. Here is my markup:

<html>
    <head>
        <title></title>
        <script src=".19.6/dist/system.js"></script>
        <script src=".js"></script>
        <script src=".0.0-beta.0/angular2-polyfills.js"></script>
        <script src=".0.0-beta.0/Rx.js"></script>
        <script src=".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-p></first-p>
    </body>
</html>

My app.js file:

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

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

I'm trying to run a basic Angular2 app. However currently running into the following error when I run the app with live-server and npm start:

Uncaught ReferenceError: ng is not defined

I have the latest Angular2 npm installed. Here is my markup:

<html>
    <head>
        <title></title>
        <script src="https://rawgithub./systemjs/systemjs/0.19.6/dist/system.js"></script>
        <script src="https://code.angularjs/tools/typescript.js"></script>
        <script src="https://code.angularjs/2.0.0-beta.0/angular2-polyfills.js"></script>
        <script src="https://code.angularjs/2.0.0-beta.0/Rx.js"></script>
        <script src="https://code.angularjs/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-p></first-p>
    </body>
</html>

My app.js file:

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

document.addEventListener('DOMContentLoaded', function() {
    ng.bootstrap(firstComponent);
});
Share Improve this question edited Jun 4, 2017 at 13:45 Francesco Borzi 62.4k56 gold badges198 silver badges276 bronze badges asked Jan 11, 2016 at 17:26 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 1
  • 1 First you have twice angular2.dev.js, second if you want to write in ES5 you should use one of the umd bundles. Check this table. – Eric Martinez Commented Jan 11, 2016 at 18:23
Add a ment  | 

1 Answer 1

Reset to default 2

From:

https://angular.io/guide/quickstart

you are missing the boot.js file which bootstrap's the whole thing before you start using it, in your file you are using the ng object before the DOM has loaded (DOMContentLoaded). That's why you have the wrapper on:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745199006a4616233.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信