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:进入后立刻加载,初始化 4.5 模板驱动型表单 表单 <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>
提交: 点击button后没反应: 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>
响应式表单: form:
[formGroup]="form" (ngSubmit)="onSubmit(form,$event)">
formControlName="email":声明成表单控件,绑定到
加入service:报错 ERROR Error:Uncaught(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 ./ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |