Replace string value with javascript object

前端 未结 4 543
清酒与你
清酒与你 2021-01-05 16:43

I am currently making a small module for NodeJs. For which I need a small help.

I will tell it like this. I have a variable with string. It contains a string html va

4条回答
  •  甜味超标
    2021-01-05 17:24

    var binddingData=function(id,data){
        var me=this,
            arr=[];
        arr=getControlBindding(id);
        arr.forEach(function(node){
            var content=getBinddingContent(node.getAttribute('DataTemp'),data);
            binddingToHtml(node,content);
        })
    
    }
    var getControlBindding=function(id){
        var me=this;
        return document.querySelectorAll('[DataTemp]');
    }
    
    
    var getBinddingContent=function(temp,data){
        var me=this,
            res='',
            hasNull=false;
        if(temp==null||typeof temp=='undefined'){
                return res;
            }
        res= temp.replace(/\$\{([^\}]+)?\}/g, function($1, $2) { 
            if(data[$2]==null||typeof data[$2]=='undefined'){
                hasNull=true;
            }
            return data[$2]; 
        });
        return hasNull?'':res;
    
    }
    
    var binddingToHtml=function(node,content){
        var me=this;
        if(node.getAttribute('IsDateTime')){
            node.innerText='';//if u want change it to datetime string
            return;
        }
        if(node.getAttribute('AddBr') && content==''){
            node.innerText='';
            var brTag=document.createElement('br');
            node.appendChild(brTag);
            return;
        }
       node.innerText=content;
    }
    

    You use the 'tag' by calling:

    with data var data={d1:'t1',d2:'t2',d3:'t3'}

提交回复
热议问题