2

I have the following AMD module defined in the "test.js" file:

define(
    'myModule',
    function () {
    'use strict';

    return function (module) {

        function myModule(translator) {
            return {
                restrict: 'A',
                link: link
            };

            function link(scope, element, attrs) {                
            }
        }

        return myModule;
    };
});

Then I load this module using System.JS and module loads without any issues.

My question is how can I include that module in my Angular 2 TypeScript module definition?

import myModule = require('myModule');    

The above will not work as it will complain about cannot find 'myModule'.

I need to be able to access the "link" function from my AMD module inside my Angular 2 module.

Any ideas?

2
  • Why not upgrade/migrate your angular1.x module to "angular2" using ngForward github.com/ngUpgraders/ng-forward Commented Jun 20, 2016 at 13:01
  • I cannot upgrade that module as it is "attribute directive". Instead of creating a new Angular 2 component we would like to re-use the same code that is already present in version 1 by importing AMD module into the TypeScript. Commented Jun 20, 2016 at 14:03

1 Answer 1

1

Here is the Demo on how to use existing AMD legacy modules with Angular 2.

[Note: I have changed your function a little for this demo purpose]

AMD mod -

define(
  'myModule',
  function() {
    'use strict';

    return function(module) {
        return {
          restrict: 'A',
          link: link
        };

        function link(scope, element, attrs) {
          console.log("Link function called");
        }
    };
  });

And this is the System Js config -

System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  //map tells the System loader where to look for things
  map: {
    app: "./src",
    myModule: './myModule.js'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    legacy: {
      defaultExtension: 'js',
    }
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying the Demo plunker and it doesn't appear to be working
@henry74 - working for me. Can you tell me whats the error?
Am I missing something or is clicking on the associated button by each name "Brad [Say Hello]" supposed to change the Hello Angular2 header...because clicking on any of the buttons does not appear to do anything for me.
The question is how to import legacy AMD modules. It has nothing to do for a click. As you see, in app.ts we call our AMD module funciton - console.log(myModule().link());

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.