Angular随手记

今天用Angular进行开发时,想要读取input的输入值:

<mat-input-container>
    <input matInput type="number" (ng-model)="dtaDte" 
    #ctrl="ngModel"
placeholder="年月" required>
</mat-input-container>

there is no directive with 'exportAs' set to 'ngModel'

结构型指令


*是语法糖
<a *ngIf="user.login">退出</a>
相当于
<ng-template ngIf="user.login">
(还是这样的?<ng-template [ngIf]="user.login">

    <a>退出</a>
</ng-template>
ngIf源码:
//声明这是个指令
@Directve{selector:'[ngIf]'}
export class NgIf{
    private hasView = false;
    constructor(private _viewContainer:ViewContainerRef, private _template:TemplateRef<Object>){}
@Input()
set ngIf(condition :any){
    if(condition && !this.hasView){
    this.hasView = true;
    //如果条件为真,创建该模板视图
    this.viewCOntainer.createEmbeddedView(this._template);
    }
    else if(!condition && this.hasView){
    This.hasView = false;
    this.viewContainer.clear();
    }
}
}

模块

entryComponents:进入后立刻加载,初始化
exports:某些组件是给大家公用的,默认只能自己模块用
forRoot()和forChild():
ngClass:用于条件动态指定样式类,适合对样式做大量更改的情况
ngStyle:用于条件动态指定样式,适合少量更改的情况
[class.yourcondition] = “condition” 直接对应一个条件

4.5 模板驱动型表单
表单的数据绑定
ngModel
表单验证
响应式表单:FormControl, FormGroup, FormBuilder
验证器和异步验证器
自定义表单控件:表单过于复杂之后,逻辑难以清晰:拆成若干简单问题

表单

<form #f="ngForm">
  <mat-input-container class="full-width">
    <input type="text" 
    matInput 
    placeholder="快速新建一个任务"
    [(ngModel)]="desc"
    name = "desc"
    required
    >

    <button matSuffix mat-icon-button type="button"> 
      <mat-icon>send </mat-icon>
    </button>
    <mat-error>
      不能为空哦
    </mat-error>
  </mat-input-container>

</form>

<div>
  表单数据:{{f.value | json}}
</div>
<div>
  表单验证状态:{{f.valid | json}}
</div>

提交:
监听ngSubmit事件:
(ngSubmit)=”onSubmit(f, $event)”

点击button后没反应:
没有导入import { NgForm } from "@angular/forms";
ps:好像不是这个问题,是sendQuickTask()的条件写错了,this.desc.trim()没有加

import { Component, OnInit, 
  Output,
  HostListener,
  EventEmitter } from '@angular/core';
import { NgForm } from "@angular/forms";

@Component({
  selector: 'app-quick-task',
  templateUrl: './quick-task.component.html',
  styleUrls: ['./quick-task.component.scss']
})
export class QuickTaskComponent implements OnInit {

  desc: string;
  @Output() quickTask = new EventEmitter();
  constructor() { }

  ngOnInit() {
  }
  onSubmit({value, valid}, ev:Event){
    ev.preventDefault();
    console.log(JSON.stringify(value));
    console.log(JSON.stringify(valid));
  }

  @HostListener('keyup.enter') //除了点击,还要监听键盘的回车
  sendQuickTask(){
    // if(!this.desc || this.desc.length === 0 || !this.desc.trim()){
    //   return;
    // }
    console.log(this.desc);
    this.quickTask.emit(this.desc);
    this.desc = '';
  }

}

html:
<mat-input-container class="full-width">
  <input type="text" 
  matInput 
  placeholder="快速新建一个任务"
  [(ngModel)]="desc"

  >
  <button matSuffix mat-icon-button 
  type="button" (click)="sendQuickTask()"> 
    <mat-icon>send </mat-icon>
  </button>
  <mat-error>不能为空哦</mat-error>
</mat-input-container>

响应式表单:
FormControl, FormGroup, FormBuilder
验证器和异步验证器(如用户是否存在,要和服务器交互)
动态指定验证器
重写登录页面:

form:
[formGroup]="form" (ngSubmit)="onSubmit(form,$event)">
formControlName="email":声明成表单控件,绑定到

加入service:报错

ERROR ErrorUncaught(in promise):Error: StaticInjectorError(AppModule)[LoginComponent -> LoginService]:
StaticInjectorError(Platform:core)[LoginComponent -> LoginService ]:
NullInjectorError:No provider for LoginService
方法:在LogiComponent的providers里加入LoginService
ng build --prod --base-href ./
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值