如何将服务注入到指令链接函数中 AngularJS重复指令实例-如何保留链接函数以避免在多个指令实例上重复?

如何将服务注入到指令链接函数中 AngularJS重复指令实例-如何保留链接函数以避免在多个指令实例上重复?,angularjs,angularjs-directive,dry,Angularjs,Angularjs Directive,Dry,标题几乎说明了这一切,但总而言之,我想将下面的链接函数移动到一个公共位置,以便在指令的多个实例化中,我不会为每个指令提供相同的链接函数集,而是将链接函数集放在一个公共位置 我该怎么做 指令 angular.module('Risk').directive('webixUi', WebixElement); function WebixElement(){ return { restrict: 'A',

标题几乎说明了这一切,但总而言之,我想将下面的链接函数移动到一个公共位置,以便在指令的多个实例化中,我不会为每个指令提供相同的链接函数集,而是将链接函数集放在一个公共位置

我该怎么做

指令

angular.module('Risk').directive('webixUi', WebixElement);

function WebixElement(){                     
      return {
            restrict: 'A',
            scope: {
                elem: '@'
            },
            controller: function($scope, CommonService){
                $scope.getTextValue = function(obj, model, type, field){
                     CommonService.getTextValue(obj, model, type, field); 
                }
            }, 
            link: function (scope, element, attrs) {
                    
                function clearLevel(){
                    leveldiv = document.querySelector("div[name='level']");
                    leveldiv.innerHTML = '';
                    leveldiv.setAttribute('class', '');
                }
                
                function assignRiskLevel(obj){
                    l = scope.$parent.risk.likelihood;
                    t = scope.$parent.risk.technical;
                    s = scope.$parent.risk.schedule;
                    c = scope.$parent.risk.cost;
                    
                    if (validLevel(obj) && riskNotEmpty(l,t,s,c))
                    {
                        if (riskIsValid(l,t,s,c))
                        {
                            l = Number(l);
                            tech = Number(t);
                            schd = Number(s);
                            cost = Number(c);
                            c = Math.max(tech,schd,cost);
                            displayLevel(scope.$parent.riskMatrix[l][c],l,c);
                            clearDot();
                            drawDot(l,c);
                        }  
                    }
                    else
                    {
                        clearLevel();
                        clearDot();
                    }
                }
                
                function isLevelField(id){
                        return (id == 'likelihood' || id == 'technical' || id == 'schedule' || id == 'cost');
                }
                
                function clearValidation(id){
                    (document.querySelector('#'+id+ ' > div.webix_control')).classList.remove("webix_invalid");
                }
                
                function makeInvalid(id){
                    (document.querySelector('#'+id+' > div.webix_control')).classList.add("webix_invalid");
                } 
                
                function validLevel(obj){
                    return obj.getValue().charCodeAt(0) - '0'.charCodeAt(0) >= 1 
                        && obj.getValue().charCodeAt(0) - '0'.charCodeAt(0) <= 5;    
                }
               
                function riskIsValid(l,t,s,c){
                    var lvl = [l,t,s,c];
                    valid = true;
                    for (var idx = 0; idx < lvl.length; idx++)
                        if ((lvl[idx].charCodeAt(0) - '0'.charCodeAt(0) < 1) || (lvl[idx].charCodeAt(0) - '0'.charCodeAt(0) > 5))
                            valid = false;
                    return valid;
                }
                
                       
                function riskNotEmpty(l, t, s, c){
                    if (l != '' && t != '' && s != '' && c != '')
                        return true;
                    return false;
                }
               
                function clearDot(){                       
                    levelDiv = document.querySelector("div.level");
                    if (levelDiv)
                        levelDiv.parentNode.removeChild(levelDiv);
                }
                
                function fieldEmpty(elem){
                    return elem.getValue() == '' || elem.getValue().trim() == '';
                }
                
                 
                function formValid(){          
                    for (var idx = 0; idx < scope.$parent.fields.length; idx++){
                        field = scope.$parent.fields[idx];
                        if (isLevelField(field) && !validLevel(scope.$parent.risk[field]))
                            return false;
                        else if (scope.$parent.risk[field] == '')
                            return false;
                    }
                    
                    return true;
                }
                
                function validate(elem, id){
                   if (isLevelField(id) && (!validLevel(elem) || fieldEmpty(elem))){
                        makeInvalid(id);
                        clearDot();                                                     
                   }        
                   else if (typeof elem !== 'undefined' && (elem.getValue() == 0 || fieldEmpty(elem)))
                        makeInvalid(id);

                   if (formValid())
                      enableElement("#submit");
                   else
                      disableElement("#submit");
                }
               
                                                                                              
                function enableElement(id){
                    (document.querySelector(id)).removeAttribute('disabled');
                }
                
                function disableElement(id){
                    (document.querySelector(id)).setAttribute('disabled', 'disabled'); 
                }
                                
                function validCharacter(c){
                    return (c >= 32 && c <= 126);
                }
                
                
                function clearTextValue(code, obj, field){
                    if (!validCharacter(code) || obj.getValue().trim() == '') 
                    {
                        scope.$parent.risk[field] = '';
                        return;
                    }
                }     
                        
                function handleKeyPress(obj, code, attr){   
                     getTextValueAndValidate(code, obj, attr); 
                     validate(obj, attr);
                }
                
                function updateAndValidate(code, obj, attr){ 
                    clearTextValue(code, obj, attr); 
                    validate(obj, attr);
                }
                
                function getTextValueAndValidate(code, obj, field){
                    clearTextValue(code, obj, field);
                    clearValidation(field);  
                    scope.getTextValue(obj, scope.$parent, 'risk', field); 
                    validate(obj, field);
                }
                
                function drawDot(l, c){
                    document.querySelector("td[name='risk["+l+"]["+c+"]']").innerHTML 
                    = "<div class='level' style='width:15px; height:15px; background-color: black'/>";
                }
                
                function displayLevel(level, l, c){
                   leveldiv =  document.querySelector("div[name='level']");
                   if (level >= scope.$parent.risklevels.riskhigh)
                   {
                       leveldiv.innerHTML = 'H ' + l + '-' + c;
                       leveldiv.setAttribute('class', 'high'); 
                   }
                   else if (level < scope.$parent.risklevels.riskhigh  && level >= scope.$parent.risklevels.riskmedium)
                   {
                        leveldiv.innerHTML = 'M ' + l + '-' + c;
                        leveldiv.setAttribute('class', 'med'); 
                   }
                   else if (level < scope.$parent.risklevels.riskmedium)
                   {
                        leveldiv.innerHTML = 'L ' + l + '-' + c;
                        leveldiv.setAttribute('class', 'low'); 
                   }
                }
                
                function getConfig(attr, type, width, height, maxlength)
                {
                    var view;
                    if (type == "level")
                        view = "text";
                    else
                        view = type;
                        
                    var config = 
                    {
                        view: view,
                        value: scope.$parent.risk[attr],      
                        on: {
                            "onTimedKeyPress": function(code){  
                                var obj = this.eventSource || this; 
                                handleKeyPress(obj, code, attr);
                                if (type == "level")
                                    assignRiskLevel(obj); 
                            },
                            "onBlur": function(code){  
                                var obj = this.eventSource || this;  
                                updateAndValidate(code, obj, attr); 
                            }
                        },
                        responsive: true,
                        width: width,
                        height: height
                    };
                    if (maxlength)
                        config.attributes = {maxlength : maxlength};
                    return config;
                };

                scope.$parent[attrs.webixUi] = getConfig(attrs.webixUi, attrs.type, attrs.width, attrs.height, attrs.hasOwnProperty('maxlength')? attrs.maxlength: null);
            }
      }            
}
angular.module('Risk')。指令('webixUi',WebixElement);
函数WebixElement(){
返回{
限制:“A”,
范围:{
元素:“@”
},
控制器:功能($scope,CommonService){
$scope.getTextValue=函数(对象、模型、类型、字段){
getTextValue(对象、模型、类型、字段);
}
}, 
链接:函数(范围、元素、属性){
函数clearLevel(){
leveldiv=document.querySelector(“div[name='level']”);
leveldiv.innerHTML='';
leveldiv.setAttribute('class','');
}
功能级别(obj){
l=范围。$母公司风险。可能性;
t=范围。$parent.risk.technical;
s=范围$parent.risk.schedule;
c=范围。$parent.risk.cost;
if(有效等级(obj)和风险等级(l、t、s、c))
{
如果(风险有效(l、t、s、c))
{
l=数量(l);
tech=数量(t);
schd=数量;
成本=数量(c);
c=数学最大值(技术、成本、成本);
显示级别(范围$parent.riskMatrix[l][c],l,c);
clearDot();
牵引点(l,c);
}  
}
其他的
{
clearLevel();
clearDot();
}
}
函数IsLevel字段(id){
返回值(id=‘可能性’| | id=‘技术’| | id=‘计划’| | id=‘成本’);
}
函数clearValidation(id){
(document.querySelector('#'+id+'>div.webix_control')).classList.remove(“webix_无效”);
}
函数makeInvalid(id){
(document.querySelector('#'+id+'>div.webix_control')).classList.add(“webix_无效”);
} 
功能有效级别(obj){
返回obj.getValue().charCodeAt(0)-“0”。charCodeAt(0)>=1
&&obj.getValue().charCodeAt(0)-“0”。charCodeAt(0)5)
有效=错误;
返回有效;
}
函数riskNotEmpty(l,t,s,c){
如果(l!=''&&t!=''&&s!=''&&c!='')
返回true;
返回false;
}
函数clearDot(){
levelDiv=document.querySelector(“div.level”);
如果(levelDiv)
levelDiv.parentNode.removeChild(levelDiv);
}
函数字段空(elem){
返回elem.getValue()=''| elem.getValue().trim()='';
}
函数formValid(){
对于(var idx=0;idx=32&&c=scope.$parent.risklevels.riskhigh)
{
leveldiv.innerHTML='H'+l+'-'+c;
leveldiv.setAttribute('class','high');
}
else if(级别=scope.$parent.risklevels.riskmedium)
{
<div ng-if="$root.initDone">
    <div webix-ui="risktitle" width="500" height="30" type="text"
         id="risktitle" name="risktitle" />
    </div>

<div ng-if="$root.initDone">
    <div webix-ui="riskstatement" width="500" height="97" type="textarea" 
         id="riskstatement" name="riskstatement" />
    </div>
angular.module('Risk').directive('webixUi', WebixElement);

̶f̶u̶n̶c̶t̶i̶o̶n̶ ̶W̶e̶b̶i̶x̶E̶l̶e̶m̶e̶n̶t̶(̶)̶{̶ ̶
function WebixElement(DOMops){                     
      return {
            restrict: 'A',
            scope: {
                elem: '@'
            },
            controller: function($scope, CommonService){
                $scope.getTextValue = function(obj, model, type, field){
                     CommonService.getTextValue(obj, model, type, field); 
                }
            }, 
            link: function (scope, element, attrs) {

                var clearLevel = DOMops.clearLevel;           

                /**
                function clearLevel(){
                    leveldiv = document.querySelector("div[name='level']");
                    leveldiv.innerHTML = '';
                    leveldiv.setAttribute('class', '');
                }
                **/