用类似jQuery的插件扩展AngularJS
Extending AngularJS with jQuery like plugin
我正在创建一个AngularJS指令,它将绑定到包含'htm'字符串的控制器范围上的属性。该字符串是从后端数据库中提取的。我希望将html附加到包含指令属性的元素。之后,我想深入到新创建的元素中,并用一个span包围每个"单词"。我使用类似于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%的插件是本地脚本,它是一个相当简单的端口到一个指令没有库(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>
演示相关文章:
- TableExport jquery插件:文件名和扩展名问题
- 扩展jQuery插件以更改配置
- Chrome扩展插件:runtime.last运行tabs.get/tabs.remove时出错:没有id为0的选项卡
- 扩展 Twitter Bootstrap 的模态插件
- 为什么可以't我使用镀铬扩展插件插入socket.io
- 如何使用jQuery扩展方法为元素或类选择器创建自定义插件
- Chrome开箱扩展插件消失
- 我怎么能理解从插件扩展的主干文件在哪里
- JavaScript代码和浏览器插件/扩展之间的冲突
- 如何调试 Thunderbird 插件/扩展的 JavaScript
- 传单:使用分组图层控件插件扩展自定义控件
- 如何通过插件扩展JavaScript库
- 将 ES6 插件扩展到 jQuery 原型
- 如何从插件/扩展中隐藏/保护客户端 javascript 中的会话/加密密钥
- 用类似jQuery的插件扩展AngularJS
- jQuery自动完成插件扩展-如何传递到绑定keyup事件
- 我如何在一个网页中显示所有事件,而不使用插件/扩展
- 窗口onerror过滤掉插件/扩展错误
- Internetexplorer-如何在JavaScript中制作IE插件/扩展
- 如何访问 JQuery 插件扩展方法