4

I am trying out angular2 in the ES5 way with this code:

<html>
    <head>
            <script src="https://code.angularjs.org/2.0.0-alpha.31/angular2.sfx.dev.js"></script>
            <script>
            function AppComponent() {}

            AppComponent.annotations = [
                new angular.Component({
                        selector: 'my-app'
                }),
                new angular.View({
                        template: '<h1>My first Angular 2 App</h1>'
                })
            ];

            document.addEventListener('DOMContentLoaded', function() {
              angular.bootstrap(AppComponent);
            });
            </script>
    </head>
    <body>
        <my-app></my-app>
    </body>
</html>

In chrome I am getting the nasty error:

Uncaught ReferenceError: angular is not defined

Specifically with the line that says:

 new angular.Component({
1
  • I'm stuck on this too. The Angular 2 displaying data guide uses this format, though I haven't found it in the other guides. Commented Feb 15, 2016 at 4:27

3 Answers 3

3

Try this one.

<!DOCTYPE html>
<html>

<head>
        <link rel="stylesheet" href="style.css" />
        <script src="https://code.angularjs.org/2.0.0-alpha.31/angular2.sfx.dev.js"></script>
        <script>
                function Service() {};

                Service.prototype.greeting = function() {
                        return 'hello';
                };

                var Cmp = ng.
                Component({
                        selector: 'cmp',
                        viewInjector: [Service]
                }).
                View({
                        template: '{{greeting}} world!'
                }).
                Class({
                        constructor: [Service, function Cmp(service) {
                                this.greeting = service.greeting();
                        }]
                });

                document.addEventListener('DOMContentLoaded', function() {
                        ng.bootstrap(Cmp);
                });
        </script>
</head>

<body>
        <cmp></cmp>
</body>

</html>
Sign up to request clarification or add additional context in comments.

6 Comments

I dont know if this is normal, but if i name "cmp something else", it gives me an error in chrome that "cmp" does not match any elements... what gives? is there a way to hide that error? JUst because I include a "directive" doesnt mean i have to use it right?
I think you missed any where. I have setup plunker with update name "cmp" to 'anil' but its working. Please see on plnkr example embed.plnkr.co/9Xbw3M/preview
At the time your angular code runs, angular does not exist yet. So, throw this is an error..
This does not work: plnkr.co/edit/4ZhVNHTk2kSME2DvkWTd, I changed anil to anill. Usually if the directive is not used, there shouldnt be an error.
I know but it seems to suggest that any angular directive defined must be used in the DOM to avoid that error. In angular1 i can define multiple directives/components, but not have to use them in the DOM.
|
0
use ng, not angular

    function AppComponent() {
        this.text = 'Hello';
    };

    AppComponent.annotations = [
        new ng.ComponentAnnotation({
                selector: 'my-app'
        }),
        new ng.ViewAnnotation({
                template: '<h1>{{text}}, My first Angular 2 App</h1>'
        })
    ];

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

Comments

0

Here you go this is your example working in a JSFiddle

https://jsfiddle.net/rmt7m0km/1/ with the angular being set as an external resource

<body>
    <script>
        function AppComponent() {}
            AppComponent.annotations = [
              new angular.ComponentAnnotation({
                selector: 'my-app'
              }),
              new angular.ViewAnnotation({
                template: '<h1>My first Angular 2 App</h1>'
              })
            ];
            document.addEventListener('DOMContentLoaded', function() {
              angular.bootstrap(AppComponent);
            });
    </script>
    <my-app></my-app>
</body>

I used https://angular.io/guide/setup

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.