Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 替换外部链接的角度指令_Javascript_Html_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 替换外部链接的角度指令

Javascript 替换外部链接的角度指令,javascript,html,angularjs,angularjs-directive,Javascript,Html,Angularjs,Angularjs Directive,如何创建要替换的自定义筛选器: <a href="http://www.externalsite.com">Whatever text</a> 为此: <a href="#" ng-click="openExternal('http://www.externalsite.com')">Whatever text</a> 这应该很容易,但我做不到……我有以下几点: <div ng-bind-html="item.htmltext |

如何创建要替换的自定义筛选器:

<a href="http://www.externalsite.com">Whatever text</a>

为此:

<a href="#" ng-click="openExternal('http://www.externalsite.com')">Whatever text</a>

这应该很容易,但我做不到……我有以下几点:

<div ng-bind-html="item.htmltext | myFilter"></div>


item.htmltext包含一些我想用这种方式替换的标记……我应该如何创建myFilter来替换链接?

编辑2:

有一种解决方案没有
$watch

指令优先级设置为-1(因为ngBindHtml的优先级为0),并且使用
$timeout
服务在下一个
$digest
周期中运行逻辑

编辑1:

您可以使用指令替换div中的子
a
节点。它设置
$watch
检查新

旧答案:

您可以创建一个指令来替换
,
替换:正确,
是的,
链接:功能(范围、要素、属性){
var href=attrs.href;
scope.open=函数(){
警报(href);
}
}
}
});
使用:

<a change-url href="http://www.externalsite.com">Whatever text</a>


jsIDLE:

我找到了另一个使用lodash替换所有a标记元素的解决方案:

angular.module('module').directive('changeUrl', function ($compile, $timeout) {
return {
    priority: -1,
    restrict: 'A',
    scope: true,
    link: function (scope, elem, attrs) {
        $timeout(function() {
        var links = elem.find('a');
            _.map(links, function (a) {
                var href = a.href;
                a.setAttribute('ng-click', 'openUrl(\'' + href + '\')');
                a.removeAttribute('href');
            });
            $compile(elem.contents())(scope);
        });
    }
}

}))

我不记得如何从指令中获取参数,所以我不会回答。但是在指令中加入这样的JS代码不足以重定向<代码>窗口位置=”http://www.yoururl.com";我忘了说我正在使用,所以我的be是我需要的过滤器…问题是我无法将更改url添加到我的内容中。正如我所说,它是这样显示的:因此item.htmltext包含html数据(来自restanglar),我已经更新了我的答案。它可能需要一些改进,但应该可以工作。它可以工作,但如果HTML文本包含很多链接,那就是浪费时间。没有办法只做一次吗?替换后可能正在销毁手表?
ng bind html
的优先级为0,因此您的指令应稍后运行。您还必须使用
$timeout
在下一个
$digest
周期中呈现内容。更新的小提琴没有
$watch
<a change-url href="http://www.externalsite.com">Whatever text</a>
angular.module('module').directive('changeUrl', function ($compile, $timeout) {
return {
    priority: -1,
    restrict: 'A',
    scope: true,
    link: function (scope, elem, attrs) {
        $timeout(function() {
        var links = elem.find('a');
            _.map(links, function (a) {
                var href = a.href;
                a.setAttribute('ng-click', 'openUrl(\'' + href + '\')');
                a.removeAttribute('href');
            });
            $compile(elem.contents())(scope);
        });
    }
}