Angular Note

Project Structure

node_moudules: third-party modules
src

  1. app: component, module
  2. assets: static
  3. style.scss global css style
  4. environments: 各个目标环境准备的文件
  5. main.ts 应用入口
  6. polyfills.ts: 填充库
  7. tsconfig.spec.json TypeScript编译器的配置文件

.angular-cli.json: Angular CLI的配置文件
package.json: npm配置文件

Create Component

  1. create:
ng g component components/testCpt
  1. import
    默认会在app.modules.ts里面引入
import { NewsComponent } from './component/news/news.component'

并且在放在装饰器@NgModule里面

@ngModule({
	declarations: [
	   AppComponent, NewsComponent
	],
})
  1. 就可以开始使用了
    在页面中引入

数据定义、绑定、处理

  1. angular 基于typescript, 定义字段的时候最好指定类型
username:string = 'xizhou';
  1. 绑定数据
<h1>{{username}}</h1>
  1. 访问修饰符号
  • public 所有(default)
  • protected 当前类+子类
  • private 当前类
  1. 定义对象
public userInfo:object = {
	userName:"xizhou",
	age:"27"
}

//也可以通过constructor赋值
constructor(){
	this.userName='xizhou'
}
  1. 获取对象数据
<h1>{{userinfo.userName}}</h1>
  1. 绑定属性
<div [title]="title test">
	test
</div>
  1. 绑定html熟悉
public content="<h2>html tag test</h2>"
<span [innerHTML]='content'></span>
  1. 数组定义,处理
public arr=['11','22','33'];
public list:any[]=[''];
public items:Array<any>=[''];

//对象数组
public userlist:any[]=[{
},{},{}]
<ol>
	<li *ngFor="let item of arr">
		{{item}}
	</li>
<ol>

<ol>
	<li *ngFor="let item of items">
		{{item.userName}} -- {{item.age}}
	</li>
<ol>
<ol>
	<li *ngFor="let item of items;let key = index">
		{{key}} -- {{item.userName}} -- {{item.age}}
	</li>
<ol>

条件判断

  1. ngIf
    在这里插入图片描述
    在这里插入图片描述

  2. switch
    在这里插入图片描述

  3. ngClass
    在这里插入图片描述
    在这里插入图片描述

Pipeline限制数据格式

在这里插入图片描述

事件触发

  1. 按钮触发
    在这里插入图片描述
    在这里插入图片描述

ex2:
在这里插入图片描述
在这里插入图片描述

  1. 表单触发
    • 键盘事件
      在这里插入图片描述

双向数据绑定、表单

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Create Service 实现方法共享

ng g service my-service-demo

app.modules.ts
在这里插入图片描述
在这里插入图片描述
Login.components.ts
在这里插入图片描述
应用场景:
在这里插入图片描述
在这里插入图片描述

利用Viewchild获取元素

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

组件之间的通讯

  1. 父组件给子组件传值
  • 参数,方法也可以,直接传方法名
  • 传整个组件给子组件 [parent] = ‘this’
    在这里插入图片描述
    在这里插入图片描述
  1. 子组件传给父组件
    引入子组件时候,定义一个ID
    在这里插入图片描述

在这里插入图片描述

生命周期函数

在这里插入图片描述

Angular Docs

在这里插入图片描述

RxJS

在这里插入图片描述

在这里插入图片描述
使用
在这里插入图片描述
取消异步方法的执行(案例:多次执行)Promise就不支持
在这里插入图片描述

Map, Filter

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

数据请求

//app.modules.ts
import { HttpClientModule } from '@angular/common/http'
imports: [
    BrowserModule,
    FormsModule,
    HttpClientModule
  ],

//demo.ts
import { HttpClient } from '@angular/common/http';
constructor(public http:HttpClient) { }
  getData(){
    let api = 'http://a.itying.com/api/productlist';
    this.http.get(api).subscribe((res:any)=>{
      console.log(res);
      this.list=res.result;
    });
  }

传递对象给接口

export interface ActionLog{
    policyNo: string;
    client: string;
    module: string;
}

let actionLog: ActionLog;
actionLog = {
policyNo: this.searchForm.getRawValue().policyNo,
client: "xizhou",
module: "NBWB"
}
debugger;
this.searchService.searchActionLog(actionLog)
.subscribe((data:any) => {
console.log(data);
})

searchActionLog(data: ActionLog) {
return this.post('/nb/v1/saveSearchPolicyLog', data);
}

Router

//创建带路由的项目
1 -- ng new angRouteDemo 
2 -- ? Would you like to add Angular routing? Yes

//配置路由
import { NewsComponent } from './components/news/news.component';
import { HomeComponent } from './components/home/home.component';
import { ProductComponent } from './components/product/product.component';
<body>
  <div style="text-align: center;">
    <h2>I'm root component</h2>
    <router-outlet></router-outlet>
    <br>
    <br>
    <h2>routeDemo02</h2>
    <a [routerLink]="[ '/home' ]">home</a>
    <a [routerLink]="[ '/news' ]">news</a>
  </div>
</body>
//默认跳转,匹配任意路由
  {
    path: '**', component: HomeComponent
  }

Bugs Encoutered

ng serve --open: An unhandled exception occurred: spawn EPERM
360安全卫士拦截导致,关闭退出后解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值