[英]Angular Directive bind function with & not passing arguments to controller
[英]Angular directive passing arguments to controller function within element's .html()
以下工作和控制台点记录了“ 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);
}
}
}
};
}
使用$scope.$emit
和$scope.$on
容易实现。
在您的指令中:
scope.$emit('passUrl', urlToPass);
在您的控制器中:
$scope.$on('passUrl', function (event, data) {
$log.debug(data); // received urlToPass variable from directive
})
看来我缺少的是如何将参数传递到控制器上的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”,但这显示了它是如何完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.