Reputation: 6813
This issue was resolved within the library - https://github.com/mbenford/ngTagsInput/pull/239
There is no on-tag-click handler for the ng-tags-input such that a user clicks on a tag to elicit a function call via an angular expression:
<!-- does not exist -->
<tags-input on-tag-click="handleTagClick(data)"></tags-input>
Upvotes: 2
Views: 3944
Reputation: 6813
As of mid-May 2015, this issue was resolved within the library https://github.com/mbenford/ngTagsInput/pull/239. Now you can use it with the directive like so:
<tags-input on-tag-clicked="callback()"
Upvotes: 5
Reputation: 2125
@jusopi: Thank you for this great answer, i`m an angular beginner and would have never found out how this works without your help. I translated your directives to javascript, maybe this helps somebody who is like me not using coffeescript:
implicit tag directive
my_app.directive('nxTag',
function() {
return {
restrict: 'AC',
link:
function($scope, elem, attrs) {
$scope.$tagClicked = function(data) {
return $scope.$emit('nxTag.clicked', data);
};
}
};
}
);
on-tag-click directive
my_app.directive('onTagClick', ['$parse',
function($parse) {
return {
link:
function($scope, elem, attrs) {
var clickFunc;
clickFunc = attrs.onTagClick ? $parse(attrs.onTagClick) : angular.noop;
return $scope.$on(
'nxTag.clicked',
function(evt, tagData) {
evt.preventDefault();
evt.stopPropagation();
return clickFunc($scope, {
data: tagData
});
});
}
};
}
]);
Upvotes: 1
Reputation: 6813
This solution doesn't seem ideal, but in lieu of a solution implemented in the directive itself, this was the only way I got it to work:
It requires:
A few things to note:
This is borrowed and modified straight from the project's documentation on creating custom tag templates
<div nx-tag class="tag-template">
<div>
<a href="" tabindex="-1" ng-click="$tagClicked(data)"><span>{{$getDisplayText()}}</span></a>
<a class="remove-button" ng-click="$removeTag()">✖</a>
</div>
</div>
If you notice in the above template, I am using nx-tag
, I'm creating the $tagClicked
function there.
.directive 'nxTag', ->
{
restrict: 'AC'
link: ($scope, elem, attrs)->
$scope.$tagClicked = (data)->
$scope.$emit 'nxTag.clicked', data
}
.directive 'onTagClick', ($parse)->
{
link: ($scope, elem, attrs)->
clickFunc = if attrs.onTagClick then $parse attrs.onTagClick else angular.noop
$scope.$on 'nxTag.clicked', (evt, tagData)->
evt.preventDefault()
evt.stopPropagation()
clickFunc $scope, {data: tagData}
}
<tags-input ng-model="vc.viewData.tags" template="nx-tag-item.html"
on-tag-removed="vc.save()" on-tag-added="vc.save()"
on-tag-click="vc._test(data)">
<auto-complete source="vc.getTags($query)"></auto-complete>
</tags-input>
Upvotes: 5