单击angular-schema-form生成的复选框会导致错误'无法读取属性'未定义'

时间:2016-06-16 17:32:11

标签: angularjs angular-schema-form

我在控制器中声明了以下架构和表单:

$scope.schema = {
        "type": "object",
        "properties": {
            "s4_0": {
                "type": "array",
                "title": "CHECKBOXES",
                "items": {
                    "type": "string",
                    "enum": [
                        1,
                        2,
                        3
                    ]
                }
            }
        }
    };

    $scope.form = [
        {
            "key": "s4_0",
            "type": "checkboxes",
            "titleMap": [
                {
                    "value": "1",
                    "name": "box 1"
                },
                {
                    "value": "2",
                    "name": "box 3"
                },
                {
                    "value": "3",
                    "name": "box 2"
                }
            ]
        },
        {
            "type": "submit",
            "title": "Submit"
        }
    ];

我的观点有以下表格声明:

<form name="myForm"
          sf-schema="schema"
          sf-form="form"
          ng-submit="onSubmit(myForm)"></form>

这会正确呈现复选框,但每次单击复选框时,都会出现以下错误:

angular.js:13424 TypeError: Cannot read property 'length' of undefined
at schema-form.min.js:1
at $watchCollectionAction (angular.js:16734)
at Scope.$digest (angular.js:16869)
at Scope.$apply (angular.js:17133)
at $$debounceViewValueCommit (angular.js:26702)
at $setViewValue (angular.js:26674)
at HTMLInputElement.listener (angular.js:23277)
at HTMLInputElement.dispatch (jquery-1.12.4.min.js:3)
at HTMLInputElement.r.handle (jquery-1.12.4.min.js:3)

当我将我的架构和表单粘贴到angular-schema-form游乐场时,这可以正常工作,但在我的应用程序中它不断抛出错误。同样在提交时,字段的值将保存为“false”,而不是选择的选项数组。

知道我可能做错了吗?

我很乐意发布更多可能更有帮助的信息。

1 个答案:

答案 0 :(得分:0)

这是因为我没有将模型设置为我的角度架构形式。因此,在您的作用域中添加一个模型对象,并在表单声明中添加sf-model以指向该变量:

$scope.surveymodel = {};

<form name="myForm"
          sf-schema="schema"
          sf-form="form"
          sf-model="surveymodel"
          ng-submit="onSubmit(myForm)"></form>