1

So I have files *.ts and it is easy to understand how to compile into *.js using tsc. But a question is how to compile HTML templates which I use in *.ts files?

For example, this code appears after it is compiled and launched in HTML file:

<script id="__bs_script__">//<![CDATA[
    document.write("<script async src='/browser-sync/browser-sync-client.2.14.0.js'><\/script>".replace("HOST", location.hostname));
//]]></script>
10
  • Are you using webpack? or systemjs? or another bundler? Commented Aug 17, 2016 at 9:26
  • So, I am just trying to understand the whole example from angular.io/docs/ts/latest/quickstart.html Commented Aug 17, 2016 at 9:32
  • Ok, so to be clear - are you trying to import HTML into your component's template? or are you trying to deploy the main index.html as part of your build process? Commented Aug 17, 2016 at 9:35
  • @Dmitry see if this helps blog.mgechev.com/2016/08/14/… Commented Aug 17, 2016 at 9:36
  • @Prasad Honrao thank you, I will look at. Commented Aug 17, 2016 at 9:47

1 Answer 1

1

You will need some sort of build system to import HTML before compiling your TS. One way is to use tools like webpack or systemjs to achieve this.

If you're using Webpack

You can use raw-loader to import HTML files as a string and inline them into your component's template.

Here's an example Webpack config (:

module: {
    loaders: [ 
      {
        test: /\.html$/,
        loader: 'raw-loader',
        include: /src/,
        exclude: [helpers.root('src/index.html')]
      }
    ]
}

Then, in your template you can use:

template: require('./template.name.html')

You can read more about raw-loader here: https://github.com/webpack/raw-loader

After the loader runs it will import the html and your template will end up inline inside the TS when it is transpiled. e.g. template: '<div>your imported html</div>

If you're using SystemJs

You can use the text plugin. Add it to your config using:

System.config({
  map: {
    text: 'path/to/text.js'
  }
});

Then import and inline your HTML into your component's template using:

template: require('./template.html!text');

You can read a bit more about the text plugin here: https://github.com/systemjs/plugin-text

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

2 Comments

Or use 'angular2-template-loader which processes your angular 2 typescript files and does the require for you...
Yup, though do you know that all angular2-template-loader does is swap a templateUrl tag for a template tag and then inserts a require? so the end result is the same as my example above. I removed it in my own project, because it's quite inefficient at parsing lots of templates looking for templateUrl strings and I'm OCD about build times

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.