4

I am trying to move the front-end part of a website to Angular 2. Currently I have really big ASP.NET 4.5 website.

I would like to create a separated Angular 2 project as it will be growing with time (and hopefully replace the asp.net) For now I would like to create this project in Visual Studio (2015) and use some angular components or modules in the actual ASP.NET website. Does the angular AppModule as to be in the ASP website?

I made some research but could not find answers or examples.

Am I gonna be able to do this relying on npm and system.js ? I saw that a lot of people are using gulp for copying file.

Is there a "right" way of doing this?

Any help would be highly appreciated

3
  • 1
    Not related to visual studio at all, but yes, you can accomplish this. As you have suggested you can (and should) maintain the Angular application as a separate project. The only additions that you need to make to your aspx page are including SystemJS and its configuration via script tags, importing your application via SystemJS.import, and adding a markup tag with the selector corresponding to the app's root element, say 'app', to your aspx markup. Commented Mar 21, 2017 at 0:45
  • Thank you for you answer I managed to do this using System.js.import Commented Mar 24, 2017 at 19:42
  • Really glad that worked for you. I have used that approach with AngularJS (Angular 1) apps/widgets on .aspx pages many times but had never done it specifically with Angular 2. Commented Mar 24, 2017 at 21:09

1 Answer 1

4

This question is not specific to Visual Studio, but yes, you can certainly accomplish this.

As you have suggested you can and should maintain the Angular application as a separate project.

The only additions that you need to make to your .aspx page are

  1. including SystemJS and its configuration via script tags in that page or in its Master Page (You can also do this dynamically for CMS pages and using all sorts of other strategies). For example

     <script src="loction-of-systemjs.js"></script>
     <script src="loction-of-systemjs.config.js"></script>
    
  2. Adding a markup tag with the selector corresponding to the app's root element, say 'my-embeddedable-widget', to your .aspx markup. For example

    <my-embeddedable-widget>Loading...</my-embeddedable-widget>
    
  3. Importing your application via SystemJS.import from a script tag embedded in the page containing the component selector above. For example

    <script> 
      SystemJS.import('my-embeddedable-widget')
        .catch (function(e) {
          console.error(e);
        }); // not using .bind or => here since aspx tends to imply older browser support
    </script>
    

Note that this presupposes two things

  1. that the 'my-embeddedable-widget' is set up in your SystemJS configuration. For example

    SystemJS.config({
      packages: {
        'my-embeddedable-widget': {
          main: 'main.ts' // just an example, could be main.js or anything really
        }
      }
    });
    

    If it is not you can add the config entry above as appropriate for your app (strongly recommended) or just import it directly from the path to the app's entry point such as e.g. my-embeddedable-widget/main.ts or my-embeddedable-widget/main.js.

  2. That the entry point of your widget declares all of its platform level dependencies, such as zone.js and likely various polyfills. For example

    my-embeddedable-widget/main.ts

    import 'zone.js';
    import 'core-js';
    // ...
    import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
    // ....
    

    This means that SystemJS will automatically load them when your widget is requested. While you could bring them in via separate script tags as we do with the loader itself, making them explicit dependencies of our widget by using ES Modules improves maintainability and allows us to defer loading them until they are required. Furthermore it helps further decouple the widget from the .aspx page. However, if other JavaScript on the page requires these polyfills, you may need to adjust this approach (especially with respect to zone.js because it monkey patches window.Promise)

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

1 Comment

I try to create based on above example code but its not working so can you provide working code of application on jsfiddle / plinkar will help me to implement

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.