0

I am creating directive for form controls, there is fix json with all possible from question and there options.

Html

<text-control-dir data="que.QuestionData" default="{{[_attributename]}}"></text-control-dir>

controlDirective.js

(function () {
    "use strict";

    angular
            .module("autoQuote")
            .directive('textControlDir', [textControlDir])
            .directive('selectControlDir', [selectControlDir])
            .directive('radioControlDir', [radioControlDir])
            .directive('hiddenControlDir', [hiddenControlDir]);

    function textControlDir()
    {
        return {
            transclude: true,
            restrict: 'E',
            scope: { 
                data: '=data'
            },
            template: "<div ng-transclude></div><label>{{data._text}} </label><input type='text' name='{{data._attributeName}}' id='{{data._attributeName}}' value='' >"
            ,
            link: function (scope, element, attrs)
            {
                //console.log(scope.data);
            }
        };
    }

    function selectControlDir()
    {
        return {
            transclude: true,
            restrict: 'E',
            scope: { 
                data: '=data',
                default: '=default'
            },
            template: ""
            ,
            link: function (scope, element, attrs)
            {
                consoile.log('link data');
                console.log(scope.default);
            }
        };
    }

    function radioControlDir()
    {
        console.log('here in radio directive');
        return {
            transclude: true,
            template: "<h1>Made by a radio directive!</h1>"
        };
    }

    function hiddenControlDir()
    {
        return {
            transclude: true,
            restrict: 'E',
            scope: { 
                data: '=data'
            },
            template: "<div ng-transclude></div><label>{{data._text}} </label><input type='hidden' name='{{data._attributeName}}' id='{{data._attributeName}}' value='' >"
            ,
            link: function (scope, element, attrs)
            {
                //console.log(scope.data);
            }
        };
    }

}());

I am not getting how to loop to create select options.

1

2 Answers 2

2

In your template's ng-repeat you have to use in instead of as here:

template: "<div ng-transclude></div><label>{{data._text}} </label><select type='text' name='{{data._attributeName}}' id='{{data._attributeName}}' >\n\
<option ng-repeat='ans in data.QuestionData._answerOptions'>{{ans._promptText}}</option></select>",

Your updated plnkr.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. you can see in result first four select control do not have any option. options for these will come through a call and will get updated on change. how to set here ajax call methods.
@Greatym.com That is too broad to answer actually. It would be better to post a new question about it. Although that is perfectly doable.
please see here fore new question stackoverflow.com/questions/38456404/…
@Greatym.com just added an answer check that.
Thanks Jai, How can we set selected option in ng-repeat
1

Here's a working plunker http://plnkr.co/edit/bc7cii5gkyNhhT4NS3uv?p=preview

It's better to use ngOptions directive because it's much faster!

Avoid options with label 'Please Select' - look at my example.

3 Comments

its working fine now. one more thing for here how to make ajax call on option changes
plnkr.co/edit/cQ1a8iFGRbx0kkR6x0JW?p=preview (look at selectControlDir function) or you could watch specific model in you controller and make ajax calls
how to set selected value in ngOptions using link function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.