原文出处:https://www.codeproject.com/articles/1060262/first-angular-app-with-typescript-and-visual-studi

Praveen Ruchandani,30 Nov 2015 CPOL
Rate this:
Building first Angular2 app with VS2013 and TypeScript.
  • Download FirstAngular2App.zip - 7.5 MB

Introduction

Modern application development has gone through dramatic changes in the past few years with frameworks like Angular,ReactJs and others. With ES6 changes,Typescript and Angular2 we are seeing a new wave of changes coming to the web application development. This tutorial walks through a process of creating a basic Angular2 application using Visual Studio.

Background

This tutorial tries to set up an environment to build Angular2 apps using Visual Studio 2013 and in the process describes how different pieces come together for an Angular2 app . The tools and technologies used in this tutorial are

1) Visual Studio 2013 Community Edition with .NET 4.5

2) Angular 2

3) Typescript

4) NPM

Let's get started!

Installing TypeScript

This tutorial needs typescript 1.6.2 or later to be installed with your Visual Studio . You can download typescript for Visual Studio 2013 from here

https://www.microsoft.com/en-us/download/details.aspx?id=48739

Once installed you can see the all versions installed at following location on your machine at the following location.

{Drive}\{Program Files Folder}\Microsoft SDKs\TypeScript\

You can verify the typescript version by typing the following command on command prompt.

https://www.codeproject.com/KB/scripting/1060262/TscVersion.png

If you still do not see the correct version check the PATH environment variable to make sure it is pointing to the location of the latest version on your machine.

https://www.codeproject.com/KB/scripting/1060262/EnvVariable.png

Installing Node.js/npm

npm (Node Package Manager) is required for installing Angular2 . npm is installed as part of Node.js installation. You can download Node.js installer from theNode.JS site. Once installed you can type the following command to check the node js version installed.

npm -v

https://www.codeproject.com/KB/scripting/1060262/NodeInstallation.png

Setting Up Visual Studio Project

1) Launch you Visual Studio and create a new project. Select the HTML with TypeScript template.

https://www.codeproject.com/KB/scripting/1060262/ProjectTemplate.png

2) The project will have index.html,app.ts and app.cs included in the project.

https://www.codeproject.com/KB/scripting/1060262/ProjectStructure.png

3) Under project properties update the TypeScript Build options to include module system. Angular2 exposes the API as modules so we will need a module handler to manage modules. We will be using systemjs for module loading. We will be installing it in next step.

https://www.codeproject.com/KB/scripting/1060262/ProjectProperties.png

To avoid compilation errors related to Decorators (see below) we will have to turn on theexperimentalDecoratorsflag for TypeScript compiler. You will have to modify the .csproj file manually to include the flag. Close your Visual Studio solution and open the .csproj file any text editor of your choice and add the following element to project properties.

<TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>

Here is a screenshot of the cs proj file

https://www.codeproject.com/KB/scripting/1060262/ExperimentalDeciaratorspng.png

without this you might see the following errors in you TypeScript files.

https://www.codeproject.com/KB/scripting/1060262/BuildError.png

Adding Angular2 & SystemJS Modules.

To install Angular2 navigate to you project folder from command prompt and install the following npm packages.

  1. angular2 - Angular 2 library.
  2. systemjs - an opensource library for module loading.
npm install angular2 systemjs --save --save-exact

If you click onShow All Filesin Visual Studio solution explorer,node_modulesfolder should be visible with folders for the angular2 and systemjs.

https://www.codeproject.com/KB/scripting/1060262/Dependencies.png

With all necessary dependencies installed let start writing our application.

The Application Component

We will be creating a simple contact manager app which displays the contact name,email address and contact number. Modify the contents of app.ts file to the listing below.

/// <reference path="node_modules/angular2/bundles/typings/es6-shim/es6-shim.d.ts" />
/ <reference path="node_modules/angular2/bundles/typings/angular2/angular2.d.ts" />

import {Component,View,bootstrap} from "angular2/angular2";

@Component(
    {

        selector: 'contact'
    })
@View({
        templateUrl: Contact.html'
})
  
class Contact {
    name: string;
    email: string;
    phone: string;
    constructor() {
        this.email = John.Doe@gmail.com';
        this.name = John Doe';
        this.phone = 1-800-GOOG';
    }
}

bootstrap(Contact);

Add a html file to your project and name it Contact.html. Modify the contents as below.

<div class="container">
    div>
        label forname">Name:/labelspan>{{name}}/span/div>{{email}}>{{phone}}>

>

This is a very simple component but let us look at some interesting things going on here.

TypeScript DeFinition Files

The first two lines of the app.ts are references to typed deFinition files. Typed DeFinition Files expose the public API of the a library (in this case Angular2) and are used by the compiler to resolve references for external modules. In our case we are using the @Component,@View and bootstrap modules declared in angular2.d.ts file.

Importing Modules

In the next line we are importing external modules required by our component. In Angular2 you are required to import any dependencies you need to build your component and not included by default. If you look at theangular2.d.tsfile located in node_modules\angular2\bundles\typings\angular2folder,you will notice that Component,View and Bootstrap are exported externally as angular2/angular2 module.

A basic Angular2 component consists of 3 parts

1) Component annotation

2) View Annotation

3) Classes

Classes

The Contact class defined is essentially our controller with defined properties. It has been defined using TypeScript and had three defined properties of name,email and phone. The constructor of the class initialized the name,email and phone properties which will be populated on our view.

Annotation/Decorators

The annotations are ways of adding Meta-data to the class and will be used by Angular to find and load our component into the DOM . In this case @Component defines a selector (<contact></contact>) and replaces it by the View defined in @View annotation.

View & Databinding

The view defined in our component is Contact.html. If you notice the listing for contact.html you will notice that the all the curly braces{{ }} are used for data-bindings to the component properties. The data-binding expressions will resolve to the class properties.

Bootstrapping

The final thing is to bootstrap our application. Angular will Now kNow which component to use and will load the component into the element that matches our selector in DOM.

HTML File

Modify the contents of index.html file as follows.

!DOCTYPE htmlhtml langen">
headMeta charsetutf-8" /title>First Angular App/titlelink relstylesheet" hrefapp.css" typetext/css" >
    
    script srcnode_modules/systemjs/dist/system.js"></script>
    node_modules/angular2/bundles/angular2.js"app.js"></script>

/headbodyscript>
    System.import('app.js');
    </script>

    h1>TypeScript HTML App/h1>

    idcontent"contact>/contact/body/html>

Few points to notice here

  1. If you notice here we are referencing the js files and not the .ts TypeScript files. The TypeScript files are required when compiling the project but we need references to the js files in our HTML.
  2. The TypeScript compiler will generate an app.js file from the app.ts typescript file referenced on our page.
  3. In addition we are referencing the angular2.js and system.js files in our page.
  4. We are calling the System.import method to register our angular component. During the registration process Angular2 will bootstrap the application by loading the component into the specified selector.

Running Application

Now when you run the application you should see the following in your browser

.https://www.codeproject.com/KB/scripting/1060262/Screenshot.png

History

Initial Draft- 11/29/2015

License

This article,along with any associated source code and files,is licensed underThe Code Project Open License (CPOL)

Share

About the Author

Praveen Ruchandani
United States
No Biography provided

First Angular2 App with TypeScript and Visual Studio 2013的更多相关文章

  1. Android Studio是否支持用于Android UI设计的AngularJS?

    我对AndroidStudio有疑问:AS在设计XML文件时是否支持AngularJS代码,例如:对于小动画或效果?

  2. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  3. 利用require.js与angular搭建spa应用的方法实例

    这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。

  4. 详解Angular动态组件

    本文主要介绍了Angular动态组件,对此感兴趣的同学,可以亲自实验一下。

  5. 详解如何使用webpack+es6开发angular1.x

    本篇文章主要介绍了详解如何使用webpack+es6开发angular1.x,具有一定的参考价值,有兴趣的可以了解一下

  6. angular2系列之路由转场动画的示例代码

    本篇文章主要介绍了angular2系列之路由转场动画的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 一种angular的方法级的缓存注解(装饰器)

    本篇文章主要介绍了一种angular的方法级的缓存注解(装饰器),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. 动手写一个angular版本的Message组件的方法

    本篇文章主要介绍了动手写一个angular版本的Message组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. angular forEach方法遍历源码解读

    这篇文章主要为大家详细了angular forEach方法遍历源码,forEach()方法用于遍历对象或数组,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Angular的MVC和作用域

    本文主要Angular的MVC和作用域进行详细分析介绍,具有一定的参考价值,下面跟着小编一起来看下吧

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部