【问题标题】:Use jquery-steps with FormGroup in Angular2在 Angular2 中使用带有 FormGroup 的 jquery-steps
【发布时间】:2017-03-27 17:31:07
【问题描述】:

请参阅以下示例。我已经将jqueryjquery-steps 加载到项目中并且它正在工作。但是,在呈现视图后,更改输入框中的数据不会更新表单组mainForm 中的值。我相信原因是jquery-steps 动态删除并重建了表单的 html,因此表单组不再链接到 DOM。

jquery-steps 重构 html 后,有什么方法可以将 FormGroup mainForm 重新绑定到 DOM?

我读到了ComponentResolverViewContainerRef,它应该在哪里使用它们?你能给我一个例子如何在这种情况下使用它们吗?

谢谢!

<pre>{{ mainForm.value | json }}</pre>

<form [formGroup]="mainForm" id="mainForm" action="#">
    <h1>Account</h1>
    <div>
        <label for="userName">User name *</label>
        <input formControlName="userName" type="text" class="required">
        <label for="password">Password *</label>
        <input formControlName="password" type="text" class="required">
        <label for="confirm">Confirm Password *</label>
        <input formControlName="confirm" type="text" class="required">
        <p>(*) Mandatory</p>
    </div>
    <h1>Finish</h1>
    < div>
        <input formControlName="acceptTerms" type="checkbox" class="required"> 
        <label for="acceptTerms">I agree with the Terms and Conditions.</label>
    </div>
</form>
import {Component, AfterContentInit} from "@angular/core";
import {FormBuilder, Validators, FormGroup} from "@angular/forms";

@Component({
    templateUrl: 'main-view.template.html'
})
export class MainViewComponent implements AfterContentInit {

    private mainForm: FormGroup;

    constructor(private formBuilder: FormBuilder) {
        this.mainForm = this.formBuilder.group({
            userName: ['', Validators.required],
            password: ['', Validators.required],
            confirm: ['', Validators.required],
            acceptTerms: ['', Validators.required],
        });
    }

    ngAfterContentInit() {
        $("#mainForm").steps();
    }
}

【问题讨论】:

    标签: angular typescript jquery-steps


    【解决方案1】:

    这不起作用的主要原因是 jquery-steps 插件删除了您的 html 标记。

    在 angular2 中使用 jquery 是个坏主意,但如果你想让它工作,我可以稍微修改一下库

    jquery.steps.js

    function render(wizard, options, state) {
    +    var contentWrapper = $('<{0} class=\"{1}\"></{0}>'.format(options.contentContainerTag, "content " + options.clearFixCssClass));
    +    contentWrapper.append(wizard.children());
        // Create a content wrapper and copy HTML from the intial wizard structure
        var wrapperTemplate = "<{0} class=\"{1}\">{2}</{0}>",
            orientation = getValidEnumValue(stepsOrientation, options.stepsOrientation),
            verticalCssClass = (orientation === stepsOrientation.vertical) ? " vertical" : "",
    -       //contentWrapper = $(wrapperTemplate.format(options.contentContainerTag, "content " + options.clearFixCssClass, wizard.html())),
    

    另请参阅Plunker Example

    【讨论】:

    • 嗨 yurzui .. 我需要禁用继续按钮,直到我满足确切的条件..ex:不填写用户名用户不能允许继续(禁用按钮)。在角度上下文中我该怎么做?谢谢
    猜你喜欢
    • 2017-05-04
    • 2017-06-21
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    相关资源
    最近更新 更多