Angular2学习
1.新建项目

2.新建Model
public class TodoItem
{
public int Id { get; set; }
public string Key { get; set; }
public string Name { get; set; }
public bool IsComplete { get; set; }
public string Desc { get; set; }
}
3.数据访问接口
public interface ITodoRepository
{
IEnumerable<TodoItem> GetAll();
void Add(TodoItem item);
TodoItem Find(string key);
TodoItem Remove(string key);
void Update(TodoItem item);
}
4.接口实现
public class TodoRepository : ITodoRepository
{
//fake datasource
static ConcurrentDictionary<string, TodoItem> _todos = new ConcurrentDictionary<string, TodoItem>(); //constructor
public TodoRepository()
{
TodoItem item = new TodoItem()
{
Id=1,
Key = Guid.NewGuid().ToString(),
Name = "first todo item",
IsComplete = false,
Desc="desc content from first todo"
}; _todos.TryAdd(item.Key, item);
} //get all items
public IEnumerable<TodoItem> GetAll()
{
return _todos.Values;
} //add new item
public void Add(TodoItem item)
{
item.Id = 1;
item.Key = Guid.NewGuid().ToString();
item.Name = Guid.NewGuid().ToString().Split('-')[0].ToUpper(); _todos[item.Key] = item;
} //find the item by key
public TodoItem Find(string key)
{
TodoItem item;
_todos.TryGetValue(key, out item);
return item;
} //remove the item by key
public TodoItem Remove(string key)
{
TodoItem item;
_todos.TryGetValue(key, out item);
_todos.TryRemove(key, out item);
return item;
} //update the item by key
public void Update(TodoItem item)
{
_todos[item.Key] = item;
}
}
5.api
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using ToDoApi.Models; namespace ToDoApi.Controllers
{
[Route("api/[controller]")]
public class TodoController:Controller
{
public ITodoRepository _TodoRepository { get; set; } public TodoController(ITodoRepository todoRepository)
{
_TodoRepository = todoRepository;
} //get:api/todo
public IEnumerable<TodoItem> GetAll()
{
return _TodoRepository.GetAll();
} //get:api/todo/id
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(string id)
{
var item = _TodoRepository.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
} //put: api/todo
[HttpPost]
//[HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
public IActionResult Create(TodoItem item)
{
if (item == null)
{
return BadRequest();
} _TodoRepository.Add(item);
return CreatedAtRoute("GetTodo", new { controller = "Todo", id = item.Key }, item);
} //Update
[HttpPut("{id}")]
public IActionResult Update(string id, [FromBody] TodoItem item)
{
if (item == null || item.Key != id)
{
return BadRequest();
} var todo = _TodoRepository.Find(id);
if (todo == null)
{
return NotFound();
} _TodoRepository.Update(item);
return new NoContentResult();
} //delete:api/todo/id
[HttpDelete("{id}")]
public void Delete(string id)
{
_TodoRepository.Remove(id);
} }
}
6.Gulp文件
var gulp = require('gulp'),
path = require("path"),
livereload = require('gulp-livereload'),
connect = require('gulp-connect'),
open = require('open'),
watch=require('gulp-watch'),
port=9000;
var PATHS = {
src: 'app/**/*.ts',
typings: 'node_modules/angular2/bundles/typings/**/*.d.ts',
libs: [
"node_modules/angular2/bundles/angular2.dev.js",
"node_modules/angular2/bundles/angular2-polyfills.js",
"node_modules/angular2/bundles/router.dev.js",
"node_modules/angular2/bundles/http.dev.js",
"node_modules/systemjs/dist/system.src.js",
"node_modules/rxjs/bundles/Rx.js"
],
html: 'app/**/*.html',
dist: "dist"
};
gulp.task('clean', function (done) {
var del = require('del');
del(['dist'], done);
});
gulp.task("libs", function(){
gulp.src(PATHS.libs).pipe(gulp.dest(PATHS.dist + "/libs"));
})
gulp.task("html", function(){
gulp.src(PATHS.html).pipe(gulp.dest(PATHS.dist));
// .pipe(watch())
//.pipe(connect.reload());
})
gulp.task('ts2js', function () {
var typescript = require('gulp-typescript');
var tsResult = gulp.src([PATHS.src, PATHS.typings])
.pipe(typescript({
noImplicitAny: true,
module: 'system',
target: 'ES5',
emitDecoratorMetadata: true,
experimentalDecorators: true
}));
return tsResult.js.pipe(gulp.dest(PATHS.dist));
});
gulp.task('play', ['html', 'libs', 'ts2js'], function () {
var http = require('http');
var _connect = require('connect');
var serveStatic = require('serve-static');
gulp.watch(PATHS.src, ['ts2js']);
gulp.watch(PATHS.html, ['html']);
var app = _connect().use( serveStatic(path.join(__dirname, PATHS.dist)) );
http.createServer(app).listen(port, function () {
open('http://localhost:' + port);
});
});
// 自动刷新不可用
gulp.task('server',['html', 'libs', 'ts2js'],function(){
gulp.watch(PATHS.src, ['ts2js']);
gulp.watch(PATHS.html, ['html']);
open('http://192.168.2.120:' + port);
connect.server({
port: port,
root:PATHS.dist,
livereload: true
});
})
7.People模块为例, people-service.ts
import { Injectable } from "angular2/core";
import { Http,Response,Headers,RequestOptions } from 'angular2/http';
@Injectable()
export class PeopleService{
names:Array<string>;
url:string="http://localhost:10003/api/todo/";
constructor(private _http:Http){
// //fade initialize
// this.names=[
// {Id:100,Key:"",Name:"Lisa",IsComplete:false,Desc:"homeless can you here me."},
// {Id:101,Key:"",Name:"Miskovsky",IsComplete:false,Desc:"calling back little bird."},
// {Id:102,Key:"",Name:"Jennifer",IsComplete:false,Desc:"Seattle is sunshine now."},
// {Id:103,Key:"",Name:"Lana Del Ray",IsComplete:false,Desc:"<<Summertime Sadness>>"},
// {Id:104,Key:"",Name:"Aili",IsComplete:false,Desc:"weibo english"},
// {Id:105,Key:"",Name:"July",IsComplete:false,Desc:"come with back love"},
// {Id:106,Key:"",Name:"Rlogue",IsComplete:false,Desc:"fall with you"}
// ];
}
//get from local
getPeopleList(){
console.log("get date from server !");
return this.names;
}
//get info from local
getPersonDetail(id:number){
return this.names.find(function(p){
return p.Id==id;
});
}
//get data from webapi
getPeople(){
return this._http.get(this.url);
// .map(res=>res.json.data)
// .catch(function(error:Response){ console.log(error); });
}
//get info from webapi
getPerson(key:string){
return this._http.get(this.url+key);
}
addPerson(){
let body = JSON.stringify({
Id: 2,
"Key": "dba980a7-555b-44b8-9043-91fc22adc00b",
Name: "first todo item",
"IsComplete": false,
Desc: "desc content from first todo"
});
let headers = new Headers({'Content-type': 'application/json;'});
let options = new RequestOptions({ headers: headers });
console.log(body);
console.log(headers);
console.log(options);
return this._http.post(this.url, body, options).subscribe(resp=>console.info(resp););
}
};
8.people列表模块 people.ts
import { Component } from "angular2/core";
import { bootstrap } from "angular2/platform/browser";
import { HTTP_PROVIDERS } from "angular2/http";
import { PeopleService } from "people/people-service.js";
import { ROUTER_PROVIDERS,RouteParams,Router} from "angular2/router";
import { InfiniteScroll } from "components/infinite-scroll/infinite-scroll.js";
@Component({
selector:"app-people",
providers:[HTTP_PROVIDERS,PeopleService,Location],
templateUrl:"people/people.html",
// template:"<h1>PEOPLE</h1>",
directives:[InfiniteScroll],
styles:[`button {border: 1px solid Red;}`]
})
export class _People implements OnInit{
name:string;
age:number;
names:Array<string>;
isBusy:boolean=false;
constructor(private _people_service:PeopleService,private _router:Router){
this.name="Lisa Miskovsky";
this.age=27;
//Observable
_people_service.getPeople().subscribe(resp=>this.names=resp.json());
}
SayHello(private _name:string){
this.name=_name;
console.log("Hello IM:"+_name);
}
viewInfo(private _id:number){
this._router.navigate( ["Person", { id:_id }] );
}
onScroll(){
console.log("scrolled :"+new Date().getTime());
if(this.isBusy) return;
this.isBusy=true;
console.log("busy:"+this.isBusy);
// this.names=this.names.concat(this._people_service.getPeopleList());
this._people_service.getPeople().subscribe(resp=>this.names=this.names.concat(resp.json());
this.isBusy=false;
console.log("busy:"+this.isBusy);
console.log("load more over !");
}
}
export function render_people(){
bootstrap(_People, [ROUTER_PROVIDERS]).catch(err=>console.log(err));
}
9.people模块的页面展现(模拟滚动加载)
<section style="margin-top: 20px;"> <div infinite-scroll style="height:500px;overflow-y: scroll;"
[infiniteScrollDistance]="1"
[infiniteScrollThrottle]="500"
(scrolled)="onScroll()"
[scrollWindow]="false"> <div *ngFor="#person of names;#_index=index">
<p *ngIf="person" style="height:auto;border:1px solid #000;padding:5px;">
{{_index+1}}、{{person.Id}}-{{person.Name}} <br/>
<span>{{person.Key}}</span><br/>
<span>{{person.Desc}}</span><br/>
<button (click)="SayHello(person.Name)">SayHello</button>
<button (click)="viewInfo(person.Key)">ViewInfo</button>
</p>
</div> </div> </section>
11.源码
http://git.oschina.net/gaojinbogit/ng2-demo
12.angualr2 typescript学习资料
--ng2.0
文件上传api http://valor-software.com/ng2-file-upload/
系列博客 http://www.cnblogs.com/1wen/p/5446863.html
快速上手 http://cnodejs.org/topic/55af2bc4911fb957520eacef
推酷站点 http://www.tuicool.com/articles/mi6rmuB
中文社区 http://www.angularjs.cn/A2pG
语法清单 https://segmentfault.com/a/1190000004071388
深度开源 http://www.open-open.com/lib/list/396
一些资料 https://segmentfault.com/a/1190000003761054
大量资料 https://github.com/timjacobi/angular2-education
1.x升级2 http://www.wtoutiao.com/p/C34jXG.html
--typescript
ES6简介 http://es6.ruanyifeng.com/#docs/intro
博客 http://www.cnblogs.com/smartkid/archive/2012/10/05/A_First_Look_Of_TypeScript.html
系列教程 http://www.cnblogs.com/tansm/p/TypeScript_Handbook_Enums.html
GitBook https://www.gitbook.com/book/zhongsp/typescript-handbook/details
tsc转职 http://www.cnblogs.com/crazylights/p/5234884.html
Angular2学习的更多相关文章
- Angular2学习笔记(1)
Angular2学习笔记(1) 1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之 ...
- angular2学习--根模块
最近有时间,学习一下angular2,根据自己的理解添加一些自己的理解,有什么不对的地方请指教, 学习的地址是https://angular.cn/ 下边是分享一下我学习过程 angular2和ang ...
- Angular2学习笔记(1)——Hello World
1. 写在前面 之前基于Electron写过一个Markdown编辑器.就其功能而言,主要功能已经实现,一些小的不影响使用的功能由于时间关系还没有完成:但就代码而言,之前主要使用的是jQuery,由于 ...
- angular2框架搭建,angular-cli使用,angular2学习
angular红红火火很多年了,一眨眼ng4都出来了,我也只能叹息前端的日新月异,以及感叹自己永远追赶不上时代的步伐,但是没关系,一个优秀的前端不在于他懂的无数的框架,而在于遇到问题时候懂得如何学习, ...
- angular2学习资源汇总
文档博客书籍类 官方网站: https://angular.io 中文站点: https://angular.cn Victor的blog(Victor是Angular路由模块的作者): https: ...
- Angular2学习笔记——路由器模型(Router)
Angular2以组件化的视角来看待web应用,使用Angular2开发的web应用,就是一棵组件树.组件大致分为两类:一类是如list.table这种通放之四海而皆准的通用组件,一类是专为业务开发的 ...
- Angular2学习笔记——Observable
Reactive Extensions for Javascript 诞生于几年前,随着angular2正式版的发布,它将会被更多开发者所认知.RxJs提供的核心是Observable对象,它是一个使 ...
- Angular2学习笔记——在子组件中拿到路由参数
工作中碰到的问题,特此记录一下. Angular2中允许我们以`path\:id\childPath`的形式来定义路由,比如: export const appRoutes: RouterConfig ...
- Angular2学习笔记——NgModule
在Angular2中一个Module指的是使用@NgModule修饰的class.@NgModule利用一个元数据对象来告诉Angular如何去编译和运行代码.一个模块内部可以包含组件.指令.管道,并 ...
随机推荐
- Sublime Text 3运行JavaScript控制台
Node.js是一个基于Chrome JavaScript运行时建立的平台,小巧方便搭建.运行的端口可以在浏览器上运行,显示效果,但每次用浏览器也挺麻烦,我们这里讲的是在sublime text2中配 ...
- java问题整理
1.一个“.java”源文件中是否可以包括多个类(不是内部类)?有什么限制? 答:可以有多个类.但只能有一个public类.并且public类名必须与文件名相一致. 2.Java有没有goto? ...
- Linux数据写操作改进
Linux的IO操作中数据的写函数int nwrite = write(int fd,void* buf ,int len)表示向fd文件描述符写入len个字节长度的数据报文,但是这并不能保证真正向内 ...
- 关于fork( )函数父子进程返回值的问题
fork()是linux的系统调用函数sys_fork()的提供给用户的接口函数,fork()函数会实现对中断int 0x80的调用过程并把调用结果返回给用户程序. fork()的函数定义是在init ...
- iPhone、iPad默认按钮样式问题
iPhone.iPad默认按钮样式问题 解决方法给按钮元素添加一个-webkit-appearance: none;具体代码 input[type="button"], input ...
- VIM快捷键(转)
VIM快捷键:光标移动:四个方向 kh 0 l j ctrl+f, ctrl+b 向下翻页,向上翻页 ctrl+d, ctrl+u ...
- tableview 重用nib cell
#import "ViewController.h" #import "NewsTableViewCell.h" #define UISCREEN_HEIGHT ...
- Java高阶面试问题合集
下面总结一下在Java面试中常用的一些问题,不具体解答,我只附上一些精彩的博文链接. Spring IOC AOP 底层原理 JAVA的反射机制和动态代理 Java反射机制和动态代理 多线程 Spri ...
- JQuery UI进度条——Progressbar
1.先引入jquery和jquery-ui的js,例子如下: <link href="JqueryUI/jquery-ui.css" rel="stylesheet ...
- 符合搜索引擎SEO规则的HTML代码
实话说,部落在有时候,也经常会修改一下自己的主题,当然,很多时候,对自己修改过后的主题,会通过查看源代码的方式,来查看自己HTML代码,很多时候,也没有去刻意对代码进行符合搜索引擎SEO规则的优化,而 ...