[英]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.