简体   繁体   English

Angular JS指令:如何设置选定值或默认值

[英]Angular js directive : How to set selected value or default value

I created directive form controls (tesxt, select, radio). 我创建了指令表单控件(tesxt,select,radio)。 In directive I am passing a value that I want access in directive function and if this value is not set or empty then set default value from question (data._pageAttributes.defaultValue) 在指令中,我传递了要在指令函数中访问的值,如果未设置此值或该值为空,则从问题中设置默认值(data._pageAttributes.defaultValue)

HTML 的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="94403"></text-control-dir>
                        </div>  
                        <div ng-if="que.QuestionData._fieldType === 'select'" >
                            <select-control-dir data="que.QuestionData" default="2016"></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 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',
                default: '=default'
            },
            template: "<div ng-transclude></div><label>{{data._text}} </label><input ng-model='answer.PC' type='text' name='{{data._attributeName}}' id='{{data._attributeName}}' value='' >"
            ,
            link: function (scope, element, attrs)
            {
                console.log('default');
                console.log(attrs.default);
                if(attrs.default == '')
                {
                    attrs.default = data._pageAttributes.defaultValue;
                }

            }
        };
    }

    function selectControlDir()
    {
        return {
            transclude: true,
            restrict: 'E',
            scope: {
                data: '=data'
            },
            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._answerOptions'>{{ans._promptText}}</option></select>"
            ,
            link: function (scope, element, attrs)
            {
                //console.log("scope.data.QuestionData", scope.data.QuestionData);
            }
        };
    }

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

    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);
            }
        };
    }

}());

CA.js file has all question and default value that we are looping for form questions. CA.js文件包含所有问题和我们正在为表单问题循环的默认值。

In directive I want to write operation to check whether value passed in default attribute or not. 在指令中,我想编写操作来检查值是否在默认属性中传递。 if no then get value from question.que and set selected in input and select control. 如果否,则从question.que获取值并在输入中设置select并选择control。

 link: function (scope, element, attrs)
        {
            if(attrs.default == '')
            {
                attrs.default = data._pageAttributes.defaultValue;
            }

        }

but this is not working for me. 但这对我不起作用。 Here I have set default value hardcoded for text(Zip code)94403 and for Vehicle year 2016 在这里,我为文本(邮政编码)94403和车辆年份2016设置了硬编码的默认值

For zip code : 94403 should be filled in input box and if not this is blank then 35004 (from CA.json) 对于邮政编码:94403应该在输入框中填写,如果不是空白,则为35004(来自CA.json)

If you don't set the default it will be undefined not an empty string 如果您没有设置默认值,它将是undefined不是empty string

link: function (scope, element, attrs)
       {
           if(attrs.default)
           {
               attrs.default = scope.data._pageAttributes.defaultValue;
           }

       }

It will check both empty and undefined . 它将检查为emptyundefined

You could use an ng-model instead of default and set the value of the ng-model in your controller if the data corresponding to the HTML element is changed trhe ng-model will change automatically. 如果与HTML元素对应的数据已更改,则可以使用ng-model代替默认值,并在控制器中设置ng-model的值。

http://jsfiddle.net/halirgb/Lvc0u55v/ http://jsfiddle.net/halirgb/Lvc0u55v/

<input ng-model="example">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM