2

I am using this as an example: JSFiddle - angular2 example My goal is to use angular4 in a single html file and create a single page application without webpack and pipes.

And this is my code, which is currently not working.

<!DOCTYPE html>
<html>
   <head>
      <script type="text/javascript" src="https://unpkg.com/[email protected]/client/shim.min.js"></script>
      <script type="text/javascript" src="https://unpkg.com/[email protected]/dist/zone.min.js"></script>
      <script type="text/javascript" src="https://unpkg.com/[email protected]/bundles/Rx.min.js"></script>
      <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/core.umd.js"></script>
      <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/common.umd.js"></script>
      <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/compiler.umd.js"></script>
      <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser.umd.js"></script>
      <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser-dynamic.umd.js"></script>
      <script type="text/typescript">
         let { Component, NgModule } = ng.core;
         @Component({
            selector: 'my-app',
            template: `
            <h1>Hello, {{ name }}</h1>
            <button (click)="increment()">Click {{ counter }}</button>
            `,
         })
         class HomeComponent {
         counter = 0;
         name = 'Angular 2'

         increment() {
         this.counter++;
         }
         }

         const { BrowserModule } = ng.platformBrowser;

         @NgModule({
         imports:      [ BrowserModule ],
         declarations: [ HomeComponent ],
         bootstrap:    [ HomeComponent ]
         })
         class AppModule { }

         const { platformBrowserDynamic } = ng.platformBrowserDynamic;
         platformBrowserDynamic().bootstrapModule(AppModule);
      </script>
   </head>
   <body>
      <my-app></my-app>
   </body>
</html>
2
  • 1
    you are wring typescript code in browser script the script tag dosent understand the @component and @Module metadata. you need to compile it to java script for it to work Commented Nov 24, 2017 at 10:28
  • It works when I added the transpiler. Commented Nov 24, 2017 at 11:29

1 Answer 1

1

Like @nikksan says, he just needed to add the transpiler, which can be done by adding the following scripts to the end of the file.

<script src="https://cdnjs.cloudflare.com/ajax/libs/typescript/3.3.4000/typescript.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/basarat/typescript-script@master/transpiler.js"></script>

Updated to Angular 7, it looks like this:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://unpkg.com/[email protected]/client/shim.min.js"></script>
    <script type="text/javascript" src="https://unpkg.com/[email protected]/dist/zone.min.js"></script>
    <script type="text/javascript" src="https://unpkg.com/[email protected]/bundles/rxjs.umd.min.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/core.umd.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/common.umd.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/compiler.umd.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser.umd.js"></script>
    <script type="text/javascript" src="https://unpkg.com/@angular/[email protected]/bundles/platform-browser-dynamic.umd.js"></script>
</head>
<body>
<my-app></my-app>
<script type="text/typescript">
let { Component, NgModule } = ng.core;
@Component({
selector: 'my-app',
template: `
    <h1>Hello, {{ name }}</h1>
    <button (click)="increment()">Click {{ counter }}</button>
`,
})
class HomeComponent {
    counter = 0;
    name = 'Angular 2'

    increment() {
        this.counter++;
    }
}

const { BrowserModule } = ng.platformBrowser;

@NgModule({
    imports:      [ BrowserModule ],
    declarations: [ HomeComponent ],
    bootstrap:    [ HomeComponent ]
})
class AppModule { }

const { platformBrowserDynamic } = ng.platformBrowserDynamic;
platformBrowserDynamic().bootstrapModule(AppModule);
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/typescript/3.3.4000/typescript.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/basarat/typescript-script@master/transpiler.js"></script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

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.