1

I know that there are some similar questions on this topic already, but somehow none of them helped. I want to use angular.js in a project which uses requirejs and Typescript.

I have a module, where I use angular to create a service module:

/// <reference path="../../../typings/angularjs/angular.d.ts" />
...
var services = angular.module('services', []);
...
export = services;

This code compiles withouth error, but in the created js file, there is angular.js dependency is not there:

define(["require", "exports"], function(require, exports) {
...
}

And when I run the application, the browser complains: Uncaught ReferenceError: angular is not defined

My guess is, that somehow I should import the angular besides referencing it, but none of the path's I tried works.

Here is my require.js configuration, in case it is needed:

require.config({

    paths: {
        angular: '../lib/angular/angular',
        ...
    },
    shim: {
        angular : {exports : 'angular'},
        ...
    }
)}

Can u help me, what is missing here? Thanks.

2 Answers 2

5

When you use a reference comment:

/// <reference path="../../../typings/angularjs/angular.d.ts" />

You are telling the compiler "I will ensure this dependency is available at runtime".

If you want the module to be loaded, you need to use an import instead...

import angular = require('angular');

The zero-set-up way of doing this is to place the angular.d.ts file next to the actual angular.js file, so the path you specify in the import statement is the same either way - otherwise, use your require config to shimmy the path.

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

1 Comment

Thank you! A good explanation how it works, but the above posted one-liner solved my problem most easily.
1

Have you tried adding the following to the top of your file

 /// <amd-dependency path="angular"/>

This should make ts compiler add angular to the define list.

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.