表单名称在Angular JS验证中无法识别

时间:2019-01-23 08:40:45

标签: angularjs

我的代码有问题。提交表单后,我有一个用于验证检查的函数,但是函数上的条件似乎无法识别我的表单名称。这是我的代码:

模板表单部分:

<div class="modal-body">
    <form name="form.addeditcoursewareModal">

        <div class="row">
            <div class="small-12 medium-12 large-12 columns">
                <label class="stacked">
                    Question
                    <input name="question" type="text" class="form-field" placeholder="Question" ng-model="$ctrl.question.data.attributes.question" >
                </label>
            </div>
        </div>

        <div class="row">
            <div class="small-12 medium-6 large-6 columns">
                <label class="stacked">
                    Question Type: {{ $ctrl.question.data.attributes.question_type }}
                    <select name="question_type" class="form-field" id="questionType" ng-model="$ctrl.question.data.attributes.question_type" ng-change="$ctrl.getSelectedQuestionType()">
                        <option value="single">Single</option>
                        <option value="multiple">Multiple</option>
                        <option value="true_or_false">True or False</option>
                        <option value="free_text">Free Text</option>
                    </select> 
                </label>
            </div>

验证部分:

validateForm(){

    this.validatingForm = true;

    if (this.question.data.attributes.question_type === 'true_or_false'){

        this.question.data.attributes.choices.data[0].attributes.choice = 'True';
        this.question.data.attributes.choices.data[1].attributes.choice = 'False';
    }

    if (this.modalOptions.actionType == 'duplicate'){

        this.question.data.attributes.choices.data.forEach((value) => {
            delete value.id;
        });
    }

    if (this.modalOptions.type === 'multiple'){
        this.validateCheckbox();
    }

    console.log(this);

    if (this.form.addeditcoursewareModal.$invalid || this.invalidMultiple) { // ERROR DETECTED

        // Check for an invalid field
        angular.forEach(this.form.addeditcoursewareModal.$error, (field) => {

            angular.forEach(field, (errorField) => {

                if (errorField.$name === 'dynamicForm'){

                    errorField.dynamicInput.$setTouched();

                } else {
                    // Set field as touched to display invalid field message
                    if (errorField.$name === 'single') {
                        this.invalidRadio = true;
                    }
                    errorField.$setTouched();
                }
            });
        });

        this.validatingForm = false;

    } else {

这是我得到的错误: angular.js:14791 TypeError: Cannot read property 'addeditcoursewareModal' of undefined

我已经尝试过重命名表单,但是错误是相同的。

1 个答案:

答案 0 :(得分:0)

似乎您正在使用控制器作为语法。您需要在 form.addeditcoursewareModal 前面添加 $ ctrl

<form name="$ctrl.form.addeditcoursewareModal">