angular2js: Uncaught Reference Error: System is no

2020-02-12 00:56发布

问题:

I am new to angular so please help. I am getting an error while trying to run a simple code with angular with angular ng-controller tag where I am displaying a variable in html being defined in a javascript file as:

var MainController = function($scope){
$scope.message = "harsh";

and displaying in html as in this:html page

The error on chrome console is as:

Uncaught ReferenceError: System is not defined(anonymous function) @ angular2.js:3098

which points to some function in angular2js:

System.register("angular2/src/core/facade/lang", [], true, function(require, exports, module) {

I don't understand the problem. Is it related to the angular2.js which I included from angularjs.org?

回答1:

Try including the System JS file in your HTML.

<script src="https://jspm.io/system@0.16.js"></script>

Check this Github repo for any help: https://github.com/kensplanet/angularjs2-hello-world/blob/master/index.html

Angular2 way of doing it

Plunker link: http://plnkr.co/edit/36PZLTZ58bXmD4me0cpS?p=preview

index.html

<html>
    <head>
        <title>AngularJS2 Hello World Demo</title>

        <script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script>
        <script src="https://jspm.io/system@0.16.js"></script>
        <script src="https://code.angularjs.org/2.0.0-alpha.28/angular2.dev.js"></script>
    </head>

    <body>
        <harsh></harsh>

    <script>
        System.import('harsh');
    </script>
        </body>
</html>

harsh.js

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc);
    switch (arguments.length) {
        case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target);
        case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0);
        case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc);
    }
};
var angular2_1 = require('angular2/angular2');
var angularjs2Component = (function () {
    function angularjs2Component() {
        this.name = "Harsh";
    }
    angularjs2Component = __decorate([
        angular2_1.Component({
            selector: 'harsh'
        }),
        angular2_1.View({
            template: '<h1>Hello {{name}}<h1>'
        })
    ], angularjs2Component);
    return angularjs2Component;
})();
angular2_1.bootstrap(angularjs2Component);

Output:

Hello Harsh




回答2:

I got the same issue in Angular 2.0 with TypeScript.

Im my issue I was requesting url with an extra slash '/' like 'http://localhost:3000/login/' (an extra slash after 'login')



回答3:

Using https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.7/angular2-all.umd.js helped to remove this error. No external system js needed.



回答4:

This may seem kind of obvious, but make sure you've run:

npm install 

to populate the node_modules directory in your app's root directory. In particular you need:

node_modules/systemjs/dist/system.src.js


回答5:

I recently ran into the same problem by following tutorial on PluralSight and using Plunker. I solved the issue by using this script

<script src="https://code.angularjs.org/1.3.0-beta.5/angular.js" data-semver="1.3.0-beta.5" data-require="angular.js@*"></script>

instead of this:

<script src="https://code.angularjs.org/2.0.0-snapshot/angular2.js" data-require="angular.js@*" data-semver="2.0.0"></script>


回答6:

In case of you are using some Seed of AngularJS.

This issues can be if you will remove or not include something like this in index.html

  <!-- shims:js -->
  <!-- endinject -->

Sometimes developers who is not familiar with AngularJS 2 and particular seed project are thinking that they remove extra comments from HTML and even doesn't realize that it is injection.