ng serve --port 80 --disable-host-check  启动80端口,禁用host检查

要在 component 内绑定全局事件的话,可以使用 @HostListener, 它会随着 component destroy 而 unbind, 很方便的哦.

export class StoogesAppComponent implements OnInit {
@HostListener('document:click', ['$event'])
private documentClick(event: Event) {
this.globalClicked$.emit(null);
}
  @HostListener('window:resize', ['$event'])
    onResize(event) {
    console.log(event.target.innerWidth);
  }
 }

image src biding 404 注意事项

<img [src]="data" >

data = "" 就不会去加载图片( undefined, null 也是去加载哦 ), 其余情况下,模板一旦渲染就会马上去加载.

好的处理就是写个 ngIf 等待 async data 回来才渲染图片.

ng 上写 keyCode 事件

<input
(keydown.arrowUp)="$event.preventDefault()"
(keydown.shift.tab)="$event.preventDefault()"
type="text" />

看的出就是把 keyCode.key 写成驼峰式就可以监听到了. 配合 shift 也很容易写哦.

模板中如果有引用对象属性,但是对象是 null 时, 会报错哦. 可以使用 ?. 来处理,这个和 c# 6.0 语法一样

<div>{{ someObj?.value }}</div> 

文件上传,Upload file *(没有试过,有时间试试,原先用的别人封装了一层的fileUploader)

<input type="file" (change)="onFileChanged($event.target.files)" placeholder="Upload file" accept="image/*">
onFileChanged(fileList: FileList) {
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
let headers = new Headers({
"Accept": "application/json"
});
let options = new RequestOptions({ headers });
this.http.post("https://localhost:44372/api/uploadFile", formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success' + data),
error => console.log(error)
)
}
}

ng 支持 formData, 关键就是别自己去写 Content-Type header, ng 会帮我们写好的.

路由获取 params ( params 是 Matrix Url 和 :Id , 要拿 search 的话用 queryParams )

class TestComponent implements OnInit, OnDestroy {
//home/xx
private sub : Subscription;
constructor(private route: ActivatedRoute) { }
ngOnInit()
{
//监听变化
this.sub = this.route.params.subscribe(params => {
console.log(params); //{ id : "xx" }
});
//如果只是要 get 一次 value, 用快照
console.log(this.route.snapshot.params); //{ id : "xx" } //this.route.snapshot.queryParams["xxx"];
} ngOnDestroy() { this.sub.unsubscribe(); //记得要取消订阅, 防止内存泄露 (更新 : 其实 ActivatedRoute 可以不需要 unsubscribe,这一个 ng 会智能处理,不过养成取消订阅的习惯也是很好的) } }

Router :ActivateRoute :Route 

1.Router : 用于 redirect 操作

2.ActivateRoute : 用于获取 data, params 等等

3.Route : 就是我们每次注册时写的资料, 里面有 data, path

文件拖拽上传测试:

angular drag & drop 如果不要使用 plugin 的话, 可以用最基本的写法

可以参考原生 html 的例子 : http://www.w3schools.com/html/html5_draganddrop.asp

复制代码
<form [formGroup]="form" submitableForm>
<s-upload #imagesUpload [config]="uploadConfig" formControlName="images"></s-upload>
<div *ngFor="let fileData of imagesUpload.fileDatas"
draggable="true"
(dragstart)="imagesUpload.dragingFileData = fileData"
(dragend)="imagesUpload.dragingFileData = null"
(dragenter)="imagesUpload.dragingFileData && imagesUpload.moveFileData(fileData,imagesUpload.dragingFileData)"
[sDragover]="imagesUpload.dragingFileData"
class="uploadImage">
<img [src]="fileData.file" width="100px">
<i [show]="fileData.loading" class="fa fa-spin fa-spinner loading"></i>
<i (click)="imagesUpload.removeFileData(fileData)" [show]="!fileData.loading" class="fa fa-times close"></i>
</div>
</form>
复制代码
dragingFileData 用于缓存变量

(dragend) 清楚缓存, 缓存还有一个重要用途就是如果你有 2 个 upload file 的时候不允许它们 cross drag

之所以不直接使用 (dragover) 是因为它会一次触发 digest (性能优化), sDragover 是一个指令里面手动添加了 event, 这样就不会一直 digest 了.

Angular2 备忘的更多相关文章

  1. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  2. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

  3. Vi命令备忘

    备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...

  4. ExtJs4常用配置方法备忘

    viewport布局常用属性 new Ext.Viewport({ layout: "border", renderTo: Ext.getBody(), defaults: { b ...

  5. [备忘] Automatically reset Windows Update components

    这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...

  6. ECMAScript 5(ES5)中bind方法简介备忘

    一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...

  7. MFC通过txt查找文件并进行复制-备忘

    MFC基于对话框的Demo txt中每行一个23位的卡号. 文件夹中包含以卡号命名的图像文件.(fpt或者bmp文件) 要求遍历文件夹,找到txt中卡号所对应的图像文件,并复制出来. VC6.0写的. ...

  8. php 相关模块备忘

    在安装php的时候,不管是编译安装: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...

  9. 『备忘』HttpWebRequest 在 POST 提交时, 标头(Headers)丢失原因

    近来研究 HttpWebRequest —— 辅助类完成时,POST JSON数据 总会 丢失标头(Headers). HttpWebRequest POST JSON数据,分如下几步: > 将 ...

随机推荐

  1. Python练习 | WebServer

    #-*- coding:utf-8 -*- import sys, os from http.server import BaseHTTPRequestHandler, HTTPServer #--- ...

  2. C++中函数调用时的三种参数传递方式

    在C++中,参数传递的方式是“实虚结合”. 按值传递(pass by value) 地址传递(pass by pointer) 引用传递(pass by reference) 按值传递的过程为:首先计 ...

  3. 解决display none到display block 渲染时间过长的问题,以及bootstrap模态框导致其他框中input不能获得焦点问题的解决

    在做定制页面的时候,遇到这么一个问题,因为弹出框用的是bootstrap的自带的弹出框,控制显示和隐藏也是用自带的属性控制 控制显示,在触发的地方 例如botton上面加上 data-toggle=& ...

  4. xamarin for android webservice

    首先新建一个空网站,添加一个webservice服务.然后在UserWebService.cs类里编写对外服务的方法 [WebMethod] public string IsCorret(string ...

  5. java 写入数据到Excel文件中_Demo

    =======第一版:基本功能实现======= import com.google.common.collect.Maps; import org.apache.log4j.Logger; impo ...

  6. 九度oj 1002 Grading 2011年浙江大学计算机及软件工程研究生机试真题

    #include<iostream> #include<queue> #include<cstdio> #include<cstring> #inclu ...

  7. [Mysql高可用]——双主互备+keepalived

    实验架构图    实验环境 主机名 操作系统 Mysql版本 keepalived版本 主机IP VIP lyj1(Master/Slave) Red Hat release 6.5 Mysql5.6 ...

  8. step7: 输出到json文件

    调用scrapy自身的Exporter输出 编辑pipeline文件 import json from scrapy.exporters import JsonItemExporter #引入Expo ...

  9. Swift构造器链

    IDE:Xcode Version7.3.1 指定构造器: 1>名字为init的方法前没有前缀(子类重写时有override除外), 2>一个类中至少有一个指定构造器,其必须初始化类中的所 ...

  10. html/JS onload的详解

    等待页面都加载完后再执行 <!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...