如何使用 angular 模式形式 select 控制器中的选项?

How to select an option in controller using angular schema form?

我只想使用控制器中的代码来 select Angular Schema Form 中的一个选项。

我在 HTML 标记中有以下内容:

<div sf-schema=schema sf-form=form sf-model=formData></div>

现在,我想在控制器中执行此操作:

//controller.js

//This is not working
$scope.formData.select_1 = 4;
$scope.formData.select_2 = 3;

//Schema for the form
$scope.schema = 
    "select_1": {
        "type": "string",
        "enum": ["1", "2", "3", "4", "5", "6"]
    },
    "select_2": {
        "type": "string",
        "enum": ["1", "2", "3", "4", "5", "6"]
    }
$scope.form = //All the form properties here

你的代码有很多问题。

架构错误。

$scope.schema = {
    "type": "object",
    "properties": {
            "select_1": {
                "type": "string",
                "enum": ["1", "2", "3", "4", "5"]
        },
            "select_2": {
                "type": "string",
                "enum": ["1", "2", "3", "4", "5"]
        }
    }
}

您将架构定义为字符串,但您将值设置为 int。

$scope.formData.select_1 = "4";
$scope.formData.select_2 = "3";

确保在设置值之前定义了模型对象 (formData)。

$scope.formData = {};

但是,您可以只使用上述值设置模型。

$scope.formData = {select_1: "4", select_2: "3"};

这是一个带有工作代码的 Plunker。

Pluner Example