Angular学习第二天--问题记录

一、问题

1.用脚手架搭建完项目之后,缺少app.modules.ts文件,

2.解决办法:

在终端继续输入命令 ng new 项目名称 --no-standalone --routing --ssr=false

3.完整目录:

二、问题

1.问题来源,源代码:

javascript 复制代码
<form action="">

    账号:<input type="text" required #nameInp="ngModel" [(ngModel)]="formData.name">
    <!-- <p>验证结果:{{nameInp.valid}}</p> -->
    <p *ngIf="!nameInp.valid">请输入账号!!!</p>


  <label>
    密码:<input type="text" required #pwdInp="ngModel" [(ngModel)]="formData.password">
    <p>验证结果:{{pwdInp.valid}}</p>
  </label>
  <button (click)="subForm2(nameInp)">提交验证</button>
</form>

2.控制台报错

javascript 复制代码
main.ts:6 ERROR Error: NG01352: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.

3.解决办法:

在绑定 ngModel的标签上绑定 name属性即可

4.完整代码,正确代码:

javascript 复制代码
<form action="">

    账号:<input type="text" required #nameInp="ngModel" [(ngModel)]="formData.name" name="username">
    <!-- <p>验证结果:{{nameInp.valid}}</p> -->
    <p *ngIf="!nameInp.valid">请输入账号!!!</p>


  <label>
    密码:<input type="text" required #pwdInp="ngModel" [(ngModel)]="formData.password" name="password">
    <p>验证结果:{{pwdInp.valid}}</p>
  </label>
  <button (click)="subForm2(nameInp)">提交验证</button>
</form>
相关推荐
计算机魔术师1 分钟前
Anthropic 发布电商 Agent 架构与生产实践指南,并开源 commerce-agents 参考实现
前端
IMPYLH10 分钟前
HTML 的 <ol> 元素
前端·html
IMPYLH11 分钟前
HTML 的 <object> 元素
前端·html
DevOpenClub12 分钟前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
夕除16 分钟前
redis--010
笔记·分布式·学习
andrsted18 分钟前
在线刷小程序推荐
学习·微信小程序·小程序·学习方法
用户2526327534721 分钟前
请问同一台服务器能同时部署前后端项目并支持https吗?
前端
weixin_4462608531 分钟前
基于熵的选择性智能体引导:从非完美视觉语言模型教师学习自主策略
人工智能·学习·语言模型
Sincerelyplz34 分钟前
【Pipecat】基于Pipecat的voice agent实践
前端·后端·agent
lerhxx38 分钟前
为什么你的Three.js物体总是乱转?一文彻底搞懂“万向锁”与四元数
前端·three.js