javascript - 使用类似 jQuery 的插件扩展 AngularJS

标签 javascript jquery angularjs jquery-plugins angularjs-directive

我正在创建一个 AngularJS 指令,该指令将绑定(bind)到包含“htm”字符串的 Controller 作用域上的一个属性。该字符串是从后端的数据库中提取的。我希望将 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 的理解。提前致谢。

最佳答案

由于插件的 90% 是 native 脚本,因此它是一个相当简单的指令端口,没有库(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

关于javascript - 使用类似 jQuery 的插件扩展 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24812778/

相关文章:

javascript - Highcharts 桑基图,系列颜色

javascript - 如何在 $.getJSON 中写入可选参数

javascript - 猫头鹰 slider 单击下一个上一个会触发所有其他 slider 滑动

javascript - 禁用相似下拉列表之间的选定选项

javascript - 在 TAG 中带有 CSS 的 Angularjs Html

javascript - 如何使用插槽在 Vuetify 数据表中设置分组行的样式?

javascript - 子字符串方法不返回最大字符限制

javascript - jQuery 如何允许您在 jQuery 对象上使用 []?

javascript - 从 JSON 解码为标准字符串

javascript - 如何在 angular.js 中重复 ajax 调用并根据响应值停止它