Include Angular inside a TypeScript file

时间:2015-07-28 16:36:42

标签: javascript angularjs typescript

I have a type script file and want to include angular.

I have the following throwing errors when compiling the typescript as angular is not included as a referenced file or anything:

app.ts

var app = angular.module('app', []);

index.html

<!DOCTYPE html>

<html lang="en">
<head>
</head>
<body>
    <script src="Scripts/angular.min.js"></script>
    <link rel="stylesheet" href="app.css" type="text/css" />
</body>
</html>

Obviously it does not matter that my index.html file correctly states dependencies in order as the compiler has no idea where angular comes from.

How do i include the external files?

1 个答案:

答案 0 :(得分:3)

You have to declare angular variable to tell compiler something like angular will be presented at runtime. You can combine it together with downloading already existing declaration file from DefinitelyTyped github repository.

If you don't want to do that, you an just use this instead:

declare var angular:any;

But I highly recommend using DefinitelyTyped Angular.js declarations, and declarations for all other 3rd party libraries for that matter. You will get code completion support.