Angular2_学习笔记

本文介绍Angular的基本操作,包括项目创建、组件和服务的生成等,并详细解释了数据绑定、模板语法及路由配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建项目
ng new xxx
创建model
  • [ ] 每个组件都必须声明在(且只能声明在)一个 NgModule中。
  • [ ] ng generate component xxx 是Angular创建组件命令
  • [ ]  Angular CLI 在生成 HeroesComponent 组件的时候就自动把它加到了 AppModule 中。

    ##### ng generate component xxx

    // 需要先cd到正确的文件夹,之后再执行创建命令 否则会默认添加到app文件夹下

元素选择器
ng generate component heroes 命令生成的组件

即具有其特定的元素选择器 <app-heroes></app-heroes>
可以用来在其他页面调用

定义数据
数据定义一般都会放在当前组件的class类里面
普通定义可以直接=赋值
readSpan = 'Hello World!'

指定规则的定义可以通过定义一个类来修饰
定义:
export class Hero {
    id: number;
    name: string;
}
使用:
import { Hero } from './../hero';
selectedHero: Hero;

创建服务
ng generate service hero

@Injectable可以从任何地方获取数据:Web 服务、本地存储(LocalStorage)或一个模拟的数据源。


属性绑定
HTML:
<span>
  {{readSpan}}
</span>

TS:
export class NavbarComponent implements OnInit {
    readSpan = 'Hello World!'
    constructor() { }
}

模板绑定
<input type="text" [value]="bindValue.title">

双向数据绑定
<input [(ngModel)]="define.title">
解决ngModel不可用问题_app.model.ts中引入
[(ngModel)]是Angular双向数据绑定的写法,但是默认情况下他是不可用的,需要 在顶级类AppModule中导入

import { FormsModule } from '@angular/forms';

imports: [
    FormsModule
  ],

管道
<h2>{{ hero.name | uppercase }} Details</h2>

通过调用管道,对数据进行格式化

循环处理数据
<ul class="heroes">
  <li *ngFor="let hero of heroes">
  </li>
</ul>

条件处理数据
<div *ngIf="selectedHero">

</div>

动态绑定类名
[class.类名]="成立条件" 
[class.selected]="hero === selectedHero" 

父组件子组件传值
// 父组件
export class AppComponent {
  bindValue = {
    title: '我将要传递给子组件'
  }
}

<app-home [bindValue]="bindValue"></app-home> // 自定义属性

// 子组件
import { Input } from '@angular/core'; // 引入input

export class HomeComponent implements OnInit {
  @Input() bindValue;   // 用input修饰传入的数据
}

<input type="text" [value]="bindValue.title">

事件绑定
<span (click)="clickMe(this.readSpan)">
  {{readSpan}}
</span>
<br>
<span on-click="clickMe(this.readSpan)">
  {{readSpan}}
</span>

事件传递
// 父组件
<app-home (onList)="list()"></app-home>

export class AppComponent {
  list () {
    alert('我是父组件的事件')
  }
}

// 子组件
<p (click)="clickMe()">
  home works!
</p>

import { Output, EventEmitter } from '@angular/core';

@Output() onList = new EventEmitter();

clickMe (val) {
    this.onList.emit();
}

路由配置
// app.routers.ts

import { Routes, RouterModule } from "@angular/router";     // 引用依赖
import { ModuleWithProviders } from "../../node_modules/@angular/compiler/src/core";    // 引用依赖

import { HomeComponent } from "./home/home.component";  // 引用组件
import { DirectoryComponent } from "./directory/directory.component";   // 引用组件

const appRouters:Routers = [
    {
        path: '',
        component: HomeComponent
    },
    {
        path: 'directory',
        component: DirectoryComponent
    }
];

export const router: ModuleWithProviders = RouterModule.forRoot(appRouters);

// app.model.ts
import { router } from './app.routers'
@NgModule({
  imports: [
    router
  ]
})
export class AppModule { }

// app.component.html
<a [routerLink]="['/']">home</a>
<router-outlet></router-outlet>

路由参数
// app.routers.ts 定义
const appRouters:Routers = [
    {
        path: 'directory/:params',  // 定义参数
        component: DirectoryComponent
    }
];

// app.component.html 使用
<a [routerLink]="['/directory', params]">directory</a>

// app.component.js 赋值
export class AppComponent {
  params = 'params'
}

// directory.component.ts 接收
import { ActivatedRoute } from '../../../node_modules/@angular/router';

export class DirectoryComponent implements OnInit {
  title:string;
  constructor(private route:ActivatedRoute) {
    this.params = route.snapshot.params['params']
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值