简体   繁体   中英

Include Angular inside a TypeScript file

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM