Angular js:如何在指令

时间:2016-07-21 10:49:10

标签: javascript jquery angularjs

我为表单控件创建了指令,它是在循环中创建的。我动态传递元素的名称。我想使用相同的方法来创建ng-model名称,但它不起作用

HTML

<div ng-repeat="que in questions[$state.current.name]">
                        <div ng-if="que.QuestionData._fieldType === 'text'" >
                            <!-- {{answers[que.QuestionData._attributeName]}} -->
                            <text-control-dir data="que.QuestionData" default="answers.PC_1" ></text-control-dir>
                        </div>  
                        <div ng-if="que.QuestionData._fieldType === 'select'" >
                            <select-control-dir data="que.QuestionData"></select-control-dir>
                        </div>
                        <div ng-if="que.QuestionData._fieldType === 'radio'" >
                            <radio-control-dir data="que.QuestionData"></radio-control-dir>
                        </div>
                        <div ng-if="que.QuestionData._fieldType === 'hidden' && que.QuestionData._attributeName != 'CBQ'" >
                            <hidden-control-dir data="que.QuestionData"></hidden-control-dir>
                        </div>
                    </div>

controlDirective.js

        function textControlDir()
    {
        return {
            transclude: true,
            restrict: 'E',
            scope: {
                data: '=data'
            },
            template: '<div class="form-group">\n\
<label for="{{data._attributeName}}" class="col-sm-5 control-label">{{data._text}}</label>\n\
<div class="col-sm-6"><input ng-model="answers[data._attributeName]"  type="text" name="{{data._attributeName}}" class="form-control" id="{{data._attributeName}}" value="{{data._pageAttributes.defaultValue}}"></div>\n\
</div>'
            ,
            link: function (scope, element, attrs)
            {

            }
        };
    }

这里我试图让ng-model =&#34;回答[data._attributeName]&#34;在文本框中

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

0 个答案:

没有答案