danhunsaker / angular-dynamic-forms

Build Forms in AngularJS From Nothing But JSON (please see Alternatives in the README)
MIT License
379 stars 140 forks source link

$parse(attrs.ngModel)($scope) return undefined #32

Closed aszharite closed 10 years ago

aszharite commented 10 years ago

Hello,

I`m using angular 1.2.21 and the $parse(attrs.ngModel)($scope) on line 68 returns undefined so the checklist breaks. Anyone else encountered this problem?

Thanks, Alex.

danhunsaker commented 10 years ago

We'll need to have a look at your HTML (specifically, the <dynamic-form> tag) to say more about this. It sounds as though you either don't have your form model defined on the scope in your controller, or don't have ng-model set to a valid model name on your tag.

aszharite commented 10 years ago

html

<dynamic-form
    template="ActiveJob.questions"
    ng-model="formData">
</dynamic-form>

controller

here i have my model and the test data i`m using.

$scope.formData = null;

$scope.ActiveJob.questions = [
    {
        "type": "checkbox",
        "model": "checkbox",
        "label": "checkbox"
    },
    {
        "type": "checklist",
        "label": "checklist",
        "model": "checklist",
        "options": {
            "first": {
                "label": "first option"
            },
            "second": {
                "label": "second option",
                 "isOn": "on",
                 "isOff": "off"
             }
        }
    },
    {
        "type": "file",
        "model": "file",
        "label": "file"
    },
    {
        "type": "select",
        "model": "select",
        "label": "select",
        "empty": "nothing selected",
        "options": {
            "first": {
                "label": "first option"
            },
            "second": {
                "label": "second option",
                "group": "first group"
            },
            "third": {
                "label": "third option",
                "group": "second group"
            },
            "fourth": {
                "label": "fourth option",
                "group": "first group"
            },
            "fifth": {
                "label": "fifth option"
            },
            "sixth": {
                "label": "sixth option",
                "group": "second group"
            },
            "seventh": {
                "label": "seventh option"
            }, 
            "eighth": {
                "label": "eighth option",
                "group": "first group"
            },
            "ninth": {
                "label": "ninth option",
                "group": "second group"
            },
            "tenth": {
                "label": "tenth option"
            }
        }
    },
    {
        "type": "submit",
        "model": "submit",
        "label": "submit"
    },
    {
        "type": "text",
        "model": "text",
        "label": "text",
        "placeholder": "text"
    },
    {
        "type": "textarea",
        "model": "textarea",
        "label": "textarea",
        "placeholder": "textarea",
        "splitBy": "\n",
        "val": ["This array should be","separated by new lines"]
    }
];
danhunsaker commented 10 years ago

You'll need to set your model ($scope.formData in this case) to an object, not null. JavaScript is really picky about data types, and won't create an object for us.

aszharite commented 10 years ago

Managed to find the problem. You are right, it was $scope.formData = null. Changing this to $scope.formData = [] will render the form/

Thanks.

danhunsaker commented 10 years ago

No problem. I'll update the documentation to make this more clear. :smile: