angular - 当表单位于子组件中时,禁用/启用父组件中的提交按钮
问题描述
我正在使用角度反应式表单,并且我有具有表单但提交按钮位于父组件上的子组件,因此我想通过基于子组件中的表单验证单击提交按钮(父组件)来限制用户进入下一页。我的问题是父组件 ts 如何知道子组件中的表单验证已经完成
<div class="parent">
<child-form-component></child-form-component>
<button type="submit">Submuit</button>
</div>
下面是示例代码 stackblitz链接的stackblitz链接
Hello 组件有表单,父组件是应用程序组件。我们正在使用 store、actions 和 reducers,所以我不想通过 subscribe 方法。
解决方案
在这种情况下,您可以简单地使用“引用变量”并询问“引用变量”.emailform.invalid
<div>
<!--use a "reference variable" to can refered to the component-->
<hello #form></hello>
<div class="form-group">
<button class="btn btn-primary" [disabled]="form.emailForm?.invalid">Submit</button>
</div>
</div>
<!--just for "check" -->
{{form.emailForm?.value|json}}
看到“表单”是组件。该组件有一个变量“emailForm”
推荐阅读
- javascript - 如何使用 POST 值更新我的数据表。ASP.NET
- cucumber - 在黄瓜数据表的步骤定义末尾放置一个冒号 (:) 是一个好习惯吗?
- string - 如何在 Julia 中使用零填充格式化整数?
- python - 当我想将 pygame 1.9.x 安装到 python 3 时发生错误
- javascript - Node.js axios 无错误提示程序停止运行
- javascript - Vue.js 组件嵌入到另一个组件时不会出现
- excel - 工作表和 VBA 中的 INDEX/MATCH 之间的区别
- reactjs - 超过 1 次测试时反应 Jest 测试警告
- android - AudioPlaybackCapture API 的策略不起作用
- python - 从 Python 中可调用的字符串创建动态函数?