使用类似jQuery的插件扩展AngularJS

时间:2014-07-17 20:19:29

标签: javascript jquery angularjs jquery-plugins angularjs-directive

我正在创建一个AngularJS指令,该指令将绑定到包含'htm'l字符串的控制器作用域上的属性。该字符串是从后端的数据库中提取的。我希望将html附加到包含指令属性的元素。之后我想深入研究新创建的元素并用跨度围绕每个'单词'。我使用类似于jQuery高亮插件的jQuery扩展函数实现了后者。实际上我的代码是对这个插件的原始代码的修改:

jQuery.extend({
highlight: function (node, re) {
    if (node.nodeType === 3) {
        var match = node.data.match(re);
        if (match) {
            var highlight = document.createElement('span');               
            highlight.className = 'highlight';
            var wordNode = node.splitText(match.index);
            wordNode.splitText(match[0].length);
            var wordClone = wordNode.cloneNode(true);
            highlight.appendChild(wordClone);             
            wordNode.parentNode.replaceChild(highlight, wordNode);
            return 1; //skip added node in parent
        }
    } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
            !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
            !(node.tagName === 'SPAN' && node.hasAttribute('ng-class'))) { // skip if already highlighted
        for (var i = 0; i < node.childNodes.length; i++) {
            i += jQuery.highlight(node.childNodes[i], re);
        }
    }
    return 0;
}
});

jQuery.fn.highlight = function (times) {
  var pattern = "\\b([^ ]+)\\b";
  var re = new RegExp(pattern);
  return this.each(function () {
    jQuery.highlight(this, re);
 });
};

我的指令的代码:

.directive('spanner', function($compile){
var linkFn = function(scope, element)
{
   element.append(scope.spanner);
   element.highlight();
   $compile(element.contents())(scope);
};

return {
    scope: {
        spanner: '='
    },
    restrict: 'A',
    link: linkFn
  };  
});

这很好但我使用的是jQuery。我更愿意扩展与AngularJs一起提供的jQlite(或者可能以一种根本不需要扩展的方式进行扩展!)。我试图扩展jQlite但每次都失败了。有人可以提出一种方法,我可以做到这一点,而不依赖于jQuery?我认为这将大大提高我对AngularJs的理解。提前谢谢。

1 个答案:

答案 0 :(得分:1)

由于90%的插件是本机脚本,因此它是一个相当简单的端口,指向没有库(jQuery或jQlite)依赖项的指令:

app.directive('highlight', function() {
  /* define highligher variables and function */
  var pattern = "\\b([^ ]+)\\b";
  var re = new RegExp(pattern);

  var highlighter = function(node, re) {
    if (node.nodeType === 3) {
      var match = node.data.match(re);
      if (match) {
        var highlight = document.createElement('span');
        highlight.className = 'highlight';
        var wordNode = node.splitText(match.index);
        wordNode.splitText(match[0].length);
        var wordClone = wordNode.cloneNode(true);
        highlight.appendChild(wordClone);
        wordNode.parentNode.replaceChild(highlight, wordNode);
        return 1; //skip added node in parent
      }
    } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
      !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
      !(node.tagName === 'SPAN' && node.hasAttribute('ng-class'))) { // skip if already highlighted
      for (var i = 0; i < node.childNodes.length; i++) {
        i += highlighter(node.childNodes[i], re);
      }
    }
    return 0;
  }

   /* return directive link function */    
   return function(scope, elem) {
      /* initalize on element */
      highlighter(elem[0], re);
    }    

});

HTML

 <p highlight>Far far away</p>

DEMO