3

I am following the tutorial on Angular2 from https://angular.io/docs/js/latest/guide/displaying-data.html but it is not working for me. Let me know what I am missing here.

BTW - I am using ES5 only.

Code in index.html -

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Angular2 Demo</title>

    <!-- 1. Load libraries -->
    <!-- IE required polyfill -->
    <script src="node_modules/es6-shim/es6-shim.min.js"></script>

    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.umd.js"></script>
    <script src="node_modules/angular2/bundles/angular2-all.umd.js"></script>

    <script src="app/component.js"></script>
</head>
<body>

        <display></display>
</body>
</html>

In component.js -

function DisplayComponent() {
  this.myName = "Alice";
}
DisplayComponent.annotations = [
  new angular.ComponentAnnotation({
    selector: "display"
  }),
  new angular.ViewAnnotation({
    template:
       '<p>My name: {{ myName }}</p>'
  })
];

Error I am getting if I am running idex.html in browser -

ReferenceError: angular is not defined

1 Answer 1

2

The sample doesn't seem to be correct... You should use the ComponentMetadata and ViewMetadata object:

function DisplayComponent() {
  this.myName = "Alice";
}

DisplayComponent.annotations = [
  new ng.core.ComponentMetadata({
    selector: "display"
  }),
  new ng.core.ViewMetadata({
    template:
       '<p>My name: {{ myName }}</p>'
  })
];

See this plunkr for more details: https://plnkr.co/edit/biMKXl1WXsgTCxbjwcme?p=preview.

You could also use the ng.core.Component object as described below:

var DisplayComponent = ng.core.
  Component({
    selector: "display"
  })
  .View({
    template:
      '<p>My name: {{ myName }}</p>'
  })
  .Class({
    constructor: function() {
      this.myName = "Alice";
    }
  });

See this plunkr: https://plnkr.co/edit/73Hirx9aqnITZCPonltX?p=preview.

This link could give you some hints:

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

4 Comments

If the docs are not correct then what should I follow ? any help ? :)
I updated my answer with more samples. Perhaps it's typo in the docs. The Pascal Precht's article could be a great starting point ;-)
Thanks for the link, sorry was out for coffee :)
No worries ;-) In fact, the way to use ES5 with Angular2 is a bit in progress. That's why there are less docs on the subject but the Angular2 guys are working on this...

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.