Switch directive not binding ngModel

I am trying to create a general radioButton directive that will accept parameters from the controller for display:

<cs-radio-field options="gender" ng-model="genderValue"></cs-radio-field>

and the parameters will be similar to

$scope.gender = { label: "Gender", required:true,  valueList: [{ text: "Male", value: "yes" },{text:"Female", value:"no"}] };

The directive is defined as:

app.directive("csRadioField", function () {
    var templateHtml = function () {
        return '<div ng-form="myform">' +
                '<div class="control-group" class="{{options.class}}">' +
                    '<div class="control-label">{{options.label || "Radio"}} {{ options.required ? "*" : ""}} </div>' +
                    '<div class="controls">' +
                        '<div class="radio" ng-repeat="(key, option) in options.valueList">' +
                            '<label> <input type="radio" name="myfield" ng-value="option.value" ng-model="ngModel" ng-required="options.required" />{{option.text}} </label>' +
                        '</div>' +
                        '<div class="field-validation-error" data-ng-show="myform.myfield.$invalid && myform.myfield.$dirty"> ' +
                            '<div data-ng-show="myform.myfield.$error.required">{{options.label}} is required!!!</div>' +
                        '</div>' +
                    '</div>' +
                '</div>' +
            '</div>';
    };

    return {
        scope: { options: '=', ngModel: '=' },
        required: ['ngModel', '^form'],
        restrict: 'E',
        template: templateHtml,
    };
});

but ngModel is optional in the parent scope. Mostly due to new areas created by ng-repeat.

How to solve this problem?

plunker link: http://plnkr.co/edit/myS5hXwxjoDEqQI2q5Q7?p=preview

+4
source share
1 answer

Try in the template:

ng-model="$parent.ngModel"

Demo

You are right that ng-repeatcreates child areas . In fact, you are tied to the properties of child areas.

+8

Source: https://habr.com/ru/post/1528258/


All Articles