15

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?

2
  • 1
    ng-controller doesn't seem like something you'd use in Angular 2. I'd probably start here if I were you ~ angular.io/docs/ts/latest/quickstart.html Commented Nov 16, 2015 at 5:15
  • I guess this is from angular not angular 2.... Commented May 26, 2016 at 18:43

6 Answers 6

10

Try including the System JS file in your HTML.

<script src="https://jspm.io/[email protected]"></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/[email protected]/traceur-runtime.js"></script>
        <script src="https://jspm.io/[email protected]"></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


Sign up to request clarification or add additional context in comments.

5 Comments

Thanx for help it did remove the error but the message is still not being displayed
In Angular2, ng-controller is not valid. Angular2 has no controllers. It has Components instead. So, your code won't work. Syntax is not similar to 1.x
@kensplanet what are these kind of error why is it happen
@flyingHawk I am not sure which error are you referring to. But if you mean the System.js error, then including the System.js file should fix the problem.
in my case the error dissappears but the problem does not.
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')

1 Comment

This was exactly what I ran into. strange.
0

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.

Comments

0

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

Comments

0

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>

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.