以下作品和控制台点记录“post”对象,但是如何将指令中锚标记的url传递给控制器函数“itemClicked”?
HTML:
<div ng-repeat="post in posts" >
<div find-urls link-clicked="itemClicked(post)" ng-bind="post.content"><div>
</div>
控制器:
$scope.itemClicked = function(post) {
console.log(post);
};
指令:
function findUrls($compile) {
return {
restrict: 'AC',
scope: {
linkClickedCallback: '&linkClicked'
},
link: function (scope, elem, attrs) {
if (attrs.ngBind) {
scope.$watch(attrs.ngBind, _.debounce(wrapUrls));
}
if (attrs.ngBindHtml) {
scope.$watch(attrs.ngBindHtml, _.debounce(wrapUrls));
}
function wrapUrls(text) {
var linkPatterns = new Array({
pattern: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,
template: ' <a class="absolute_link" href="$1" target="_blank">$1</a>'
},
{
pattern: /(^|[^\/])(www\.[\S]+(\b|$))/ig,
template: ' <a class="absolute_link" href="http://$2" ng-click="linkClickedCallback();" target="_blank">$2</a>'
},
{
pattern: /([a-z0-9._-]+@[a-z0-9._-]+\.[a-zA-Z0-9._-]+)/ig,
template: ' <a class="absolute_link" href="mailto:$1" ng-click="linkClickedCallback();" target="_blank">$1</a>'
},
{
pattern: /(^|[^a-z0-9@\\\/._-])([a-z0-9]{0,256}\.(com|net|org|edu)([a-z0-9\/?=\-_#]{0,256})?(\b|$))/ig,
template: ' <a class="absolute_link" href="http://$2" ng-click="linkClickedCallback();" target="_blank">$2</a>'
});
var html = elem.html();
var newHtml = html;
linkPatterns.forEach((item) => {
newHtml = newHtml.replace(item.pattern, item.template);
});
if (html !== newHtml) {
elem.html(newHtml);
$compile(elem.contents())(scope);
}
}
}
};
}
答案 0 :(得分:0)
使用$scope.$emit
和$scope.$on
即可轻松实现。
在你的指令中:
scope.$emit('passUrl', urlToPass);
在您的控制器中:
$scope.$on('passUrl', function (event, data) {
$log.debug(data); // received urlToPass variable from directive
})
答案 1 :(得分:0)
看起来我缺少的是如何将参数传递给控制器上的linkClickedCallback函数。您需要通过对象{arg1:5,arg2:10}将参数传递给函数,然后以相同的顺序将它们添加到HTML中的函数,以将它们传递给控制器。
我创建了对象{link:1}以传递到linkedClickedCallback,其中1是硬编码值。当示例运行时,它输出1并在下一行输出HTML中定义的“post”对象。
<强> HTML:强>
<div ng-repeat="post in posts" >
<div find-urls link-clicked="itemClicked(link, post)" ng-bind="post.content"><div>
</div>
<强>控制器:强>
$scope.itemClicked = function(link, post) {
console.log(link);
console.log(post);
};
<强>指令:强>
function findUrls($compile) {
return {
restrict: 'AC',
scope: {
linkClickedCallback: '&linkClicked'
},
link: function (scope, elem, attrs) {
if (attrs.ngBind) {
scope.$watch(attrs.ngBind, _.debounce(wrapUrls));
}
if (attrs.ngBindHtml) {
scope.$watch(attrs.ngBindHtml, _.debounce(wrapUrls));
}
function wrapUrls(text) {
var linkPatterns = new Array(
{
pattern: /(^|[^\/])(www\.[\S]+(\b|$))/ig,
template: ' <a class="absolute_link" href="http://$2" ng-click="linkClickedCallback({link: 1});" target="_blank">$2</a>'
},
{
pattern: /([a-z0-9._-]+@[a-z0-9._-]+\.[a-zA-Z0-9._-]+)/ig,
template: ' <a class="absolute_link" href="mailto:$1" ng-click="linkClickedCallback({link: 1});" target="_blank">$1</a>'
},
{
pattern: /(^|[^a-z0-9@\\\/._-])([a-z0-9]{0,256}\.(com|net|org|edu)([a-z0-9\/?=\-_#]{0,256})?(\b|$))/ig,
template: ' <a class="absolute_link" href="http://$2" ng-click="linkClickedCallback({link: 1});" target="_blank">$2</a>'
});
var html = elem.html();
var newHtml = html;
linkPatterns.forEach((item) => {
newHtml = newHtml.replace(item.pattern, item.template);
});
if (html !== newHtml) {
elem.html(newHtml);
$compile(elem.contents())(scope);
}
}
}
};
}
我仍然需要做一些工作来捕获链接值并传递它而不是硬编码“1”,但这显示了它需要如何完成。