angularjsangular-schema-form

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


I just want to use code in controller to select an option in Angular Schema Form .

I have the following in HTML Markup:

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

Now, I want to do this in controller:

//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

Solution

  • There are a lot of issues with your code.

    The schema is wrong.

    $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"]
            }
        }
    }
    

    You defined the schema as a string, yet you're setting the value as an int.

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

    Make sure you've defined the model object (formData) before setting values.

    $scope.formData = {};
    

    However, you could have just set the model with the values above.

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

    Here's a Plunker with the working code.

    Pluner Example