一、双向数据绑定

1-1 app.module.ts中配置

  1. import {FormsModule} from '@angular/forms'
  2. imports: [
  3. BrowserModule,
  4. FormsModule
  5. ],

1-2 组件中使用

  1. <input type="text"
  2. [(ngModel)]="msg">

二、(change)

  1. <input type="text"
  2. (change)="handleChange($event)"
  3. [(ngModel)]="msg">
  4. handleChange(e){
  5. console.log(e.target)
  6. }

三、(ngModelChange)

只要ngModel依赖的值改变的时候,就会触发 ngModel一定要写在ngModelChange的前面

  1. <input type="text"
  2. [(ngModel)]="msg"
  3. (ngModelChange)="handleChange()"
  4. >
  5. <p>{{msg}}</p>
  6. <input type="checkbox"
  7. [(ngModel)]="checked"
  8. (ngModelChange)="inputChange()"
  9. >

四、ant-design

4-1 自动构建

ng add ng-zorro-antd

五、发送http

5-1 app.module.ts配置

  1. import {HttpClientModule} from '@angular/common/http'
  2. imports: [
  3. BrowserModule,
  4. HttpClientModule
  5. ],

5-2 在发送http请求的组件中配置HttpClient,作为构造函数的参数设置

  1. import { Component, OnInit } from '@angular/core';
  2. import {HttpClient} from '@angular/common/http'
  3. ...
  4. export class ContentComponent implements OnInit {
  5. public list:any
  6. constructor(public http:HttpClient) { }
  7. ngOnInit() {
  8. var url:string = "http://192.168.14.15:5000/top/playlist";
  9. this.http.get(url).subscribe(res=>{
  10. this.list = res["playlists"];
  11. })
  12. }
  13. }

六、父子组件传值

一、父组件给子组件传参

6-1 在父组件中,子组件通过属性接收父组件传递过来的参数

  1. <app-header [title]="title"></app-header>
  1. export class AppComponent {
  2. title:string = '我你爸爸';
  3. }

6-2 子组件中引入Input模块

  1. import { Component, OnInit,Input } from '@angular/core';
  2. ...
  3. //3.子组件中 @Input 接收父组件传过来的数据
  4. export class HeaderComponent implements OnInit {
  5. @Input() title:string;
  6. constructor() { }
  7. ngOnInit() {
  8. }
  9. }
  10. //4.子组件中使用父组件的数据
  11. <p>{{title}}</p>

二、子组件给父组件传参

  1. //1.子组件通过属性接收父组件的方法
  2. <app-header [run]="run"></app-header>
  3. export class AppComponent {
  4. run(id:string){
  5. console.log(id);
  6. }
  7. }
  1. //2.子组件@Input接收,调用
  2. export class HeaderComponent implements OnInit {
  3. @Input() run:any;
  4. ...
  5. handleClick(){
  6. this.run("1234")
  7. }
  8. }
  1. <button (click)="handleClick()">向父传参</button>

七、子组件通过@Output向父组件传参 —了解

  1. // 泛型:任意类型
  2. class Person<T>{
  3. emit(msg:T){
  4. console.log(msg);
  5. }
  6. }
  7. var p = new Person<string>();
  8. p.emit("hello world")
  9. //2-1 子组件引入Output和EventEmitter
  10. import { Component, OnInit,Input,Output,EventEmitter } from '@angular/core';
  11. //2-2 子组件中实例化EventEmitter
  12. @Output() private outer = new EventEmitter<string>();
  13. //2-3 子组件中通过EventEmitter实例化的对象outer,广播数据
  14. handleClick(){
  15. this.outer.emit("101001");
  16. }
  17. <button (click)="handleClick()">向父传参</button>
  18. //2-4 父组件中接收事件
  19. <app-header (outer)="getId($event)"></app-header>
  20. export class AppComponent {
  21. getId(id:string){
  22. console.log(id);
  23. }
  24. }