AngularDart:使用反应式表单生成器创建表单

2024-01-16

我正在尝试使用创建AngularDart 5, angular_forms 2。该API似乎与angular_forms 1我不知道如何创建一个FormGroup using FormBuilder and ControlGroup。下面是我的代码。

LoginComponent.dart

import 'dart:convert';

import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';

@Component(
    selector: 'login-comp',
    templateUrl: 'login_component.html',
    styleUrls: ['login_component.css'],
    directives: [formDirectives, coreDirectives])
class LoginComponent implements OnInit {
  ControlGroup ctrlGrp;

  LoginComponent() {
    this._createForm();
  }

  void _createForm() {
    final email = Control<String>('', Validators.required);
    final password = Control<String>(
        '', Validators.compose([Validators.required, Validators.minLength(8)]));

    this.ctrlGrp =
        FormBuilder.controlGroup({'email': email, 'password': password});
  }

  @override
  void ngOnInit() {}

  void submit() {}
}

登录组件.html

<div class="container d-flex justify-content-center align-times-center">
    <div class="form-wrapper text-center">
        <h2 class="text-center">Login</h2>
        <form novalidate #f="ngForm" (ngSubmit)="submit()">
            <div ngControlGroup="ctrlGrp">
                <div class="form-group">
                    <input type="email" ngControl="email" class="form-control" id="email-input"
                           placeholder="Enter Email" formControlName="email"/>
                </div>
                <div class="form-group">
                    <input type="password" ngControl="password" class="form-control" id="password-input"
                           placeholder="Enter Password" formControlName="password"/>
                </div>
            </div>
            <button type="submit" class="btn btn-primary" [disabled]="!f.valid">Submit</button>
        </form>
        <small><a [routerLink]="'/signup'">Need an Account?</a></small>
    </div>
</div>

pubspec.yaml

environment:
  sdk: '>=2.0.0 <3.0.0'

dependencies:
  angular: ^5.0.0
  ng_bootstrap: ^1.1.1
  sass_builder: ^2.0.0
  angular_router: ^2.0.0-alpha+19
  angular_forms: ^2.1.1

dev_dependencies:
  angular_test: ^2.0.0
  build_runner: ^1.0.0
  build_test: ^0.10.2
  build_web_compilers: ^0.4.0
  test: ^1.0.0
  • 我怎样才能创建一个FormGroup with angular_forms 2?
  • 如何将 from 组绑定到formGrouphtml 模板中的属性?

Thanks.


我在@ebelair 的帮助下找到了答案。

我错过了[ngFormModel]="loginForm"部分。 下面是工作代码。

登录组件.dart

@Component(
    selector: 'login-comp',
    templateUrl: 'login_component.html',
    styleUrls: ['login_component.css'],
    directives: [formDirectives, coreDirectives, routerDirectives])
class LoginComponent implements OnInit {
  ControlGroup loginForm;

  LoginComponent() {
    this._createForm();
  }

  void _createForm() {
    final email = Control<String>('', Validators.required);
    final password = Control<String>('', Validators.compose([Validators.required, Validators.minLength(8)]));

    this.loginForm = FormBuilder.controlGroup({'email': email, 'password': password});
  }

  @override
  void ngOnInit() {}

  void submit() {
    print(this.loginForm.value['email']);
    print(this.loginForm.value['password']);
  }
}

登录组件.html

<div class="container d-flex justify-content-center align-times-center">
    <div class="form-wrapper text-center">
        <h2 class="text-center">Login</h2>
        <form novalidate [ngFormModel]="loginForm" (ngSubmit)="submit()">
            <div>
                <div class="form-group">
                    <input type="email" ngControl="email" class="form-control" id="email-input"
                           placeholder="Enter Email" formControlName="email"/>
                </div>
                <div class="form-group">
                    <input type="password" ngControl="password" class="form-control" id="password-input"
                           placeholder="Enter Password" formControlName="password"/>
                </div>
            </div>
            <button type="submit" class="btn btn-primary" [disabled]="!loginForm.valid">Submit</button>
        </form>
        <small><a [routerLink]="'/signup'">Need an Account?</a></small>
    </div>
</div>

希望这可以帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

AngularDart:使用反应式表单生成器创建表单 的相关文章

随机推荐

  • iOS6旋转问题

    我知道你必须使用IOS6的新旋转方法 但似乎我写的方法不起作用 我将 plist 文件设置为允许所有旋转 但是不是肖像颠倒 然后我的里面有以下内容应用程序委托 self navController UINavigationControlle
  • 为什么我可以在 JavaScript 中定义函数之前使用该函数?

    即使在不同的浏览器中 此代码也始终有效 function fooCheck alert internalFoo We are using internalFoo here return internalFoo And here even t
  • 使用jpype启动jvm时出现分段错误

    我正在尝试在我的 Python 代码中使用 java sutime 直到昨天 代码一直运行良好 但现在以分段错误结束 问题似乎来自于 jpype startJVM getDefaultJVMPath 该函数以分段错误结束 默认的 JVM 路
  • 有条件的列的计数器缓存?

    我对计数器缓存的概念很陌生 并且我的应用程序主页之一的加载时间有些天文数字 我相信我需要继续下去 我需要实现的大多数计数器缓存都附加了某些 简单 条件 例如 这是一个常见的查询 projects employee projects wher
  • 如何使用 ocLazyLoad 进行依赖注入

    我正在使用 ocLazyLoad 并且我有一些外部角度库 例如Chart js and pascalprecht translate 并且我需要在某些路由中延迟加载它们 如您所知 对于常见的角度模块依赖注入应该是这样的 var angula
  • 如何模拟 HttpServletRequest? [复制]

    这个问题在这里已经有答案了 我有一个查找查询参数并返回布尔值的函数 public static Boolean getBooleanFromRequest HttpServletRequest request String key Bool
  • 在 Ruby on Rails 3 上上传多个文件[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我想将多张图片上传到我的 Rails 3 应用程序 我目前正在使用 Paperclip 上传图片 并且我
  • 在 ReactJS 中显示/隐藏组件

    我们现在在使用 React 的过程中遇到了一些问题 但是它kinda归结为我们如何使用 React 的一部分 我们应该如何显示 隐藏子组件 这就是我们编码的方式 这只是我们组件的片段 click function if add here i
  • 如何在 Python 3 中使用 urllib.request 下载文件?

    所以 我正在搞乱urllib request在Python 3中 我想知道如何将获取互联网文件的结果写入本地计算机上的文件 我试过这个 g urllib request urlopen http media mcw cursecdn com
  • 如何为 reStructuredText、Sphinx、ReadTheDocs 等设置自定义样式?

    我想扩展使用的主题Sphinx and 阅读文档与我自己的自定义样式 为了让我的更改得以保留 最好的方法是什么 编辑 截至 2021 年 以下答案已过时 请使用html css files 在你的conf py而不是使用版本 1 8 之后的
  • 如何为 wxWidgets OpenGL 程序启用多重采样?

    多重采样 http en wikipedia org wiki Multisample anti aliasing是一种申请方式全屏抗锯齿 FSAA 在 3D 应用中 我需要在我的 OpenGL 程序中使用多重采样 该程序当前嵌入在wxWi
  • 如何在 Jest 中模拟嵌套函数?

    我收到这个错误 Cannot find module httpsGet from functions getSecureString test js httpsGet 是我自己的功能 位于按钮处getSecureString js 并通过调
  • 使用 OuterRef 的简单子查询

    我正在尝试做一个非常简单的Subquery使用OuterRef 不是为了实际目的 只是为了让它工作 但我一直遇到同样的错误 posts models py code from django db import models class Ta
  • ASP.NET MVC 部分视图不调用我的操作

    我刚刚开始在 ASP NET MVC 上构建一个小型简单网站 在一个页面中我使用部分视图 部分视图代表一个简单的表单 应该在单击按钮时提交 如果我第一次单击它提交的时间成功并返回我的部分视图和验证消息 如果内容无效 但如果我想再试一次 则不
  • “由于共享主机中的‘LOG_BACKUP’,数据库的事务日志已满”

    我有一个 Asp Net MVC 5 网站 在共享托管计划中采用实体框架代码优先方法 它使用开源网站面板 http www websitepanel net 控制面板及其 SQL Server 面板有些限制 今天想编辑数据库的时候 遇到了这
  • Android 在 HttpClient 中收到 403 后得到响应

    我有这样的代码 HttpClient httpClient new DefaultHttpClient HttpPost httpPost new HttpPost server try JSONObject params new JSON
  • 当单个模型被多个视图共享时

    有两种观点 listView QtGui QListView tableView QtGui QTableView 我继续定义一个自定义DataModel供双方使用listView and tableView class DataModel
  • oops 的完整形式是什么? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 的完整形式是什么OOPS 有人说面向对象的编程结构其他人说面向对象的编程系统 哪一个是正确的 编程范式是OOP 面向对象编程 其他一切都可以从它
  • 材质 UI - 轮廓选择标签未正确渲染

    根据演示 Material UI 概述选择输入的标签应位于选择框的顶部边框的顶部 然而 在我的应用程序中 z index标签的似乎将其放置在顶部边框后面 因此看起来像一条线穿过标签 我几乎完全从文档中获取了代码 据我所知 没有任何样式与此输
  • AngularDart:使用反应式表单生成器创建表单

    我正在尝试使用创建AngularDart 5 angular forms 2 该API似乎与angular forms 1我不知道如何创建一个FormGroup using FormBuilder and ControlGroup 下面是我