how to copy tag text under ui-select

285 views Asked by At

I need the user to be able to select (using mouse or keyboard) a selected tag value and be able to copy the text somehow. In the following sample plunker, I need the user to be able to copy text "blue"/"red" -

ui-select tagging sample

Any suggestion?

1

There are 1 answers

0
stLmpp On

You could do this:

Create a directive to copy the value of the item to the clipboard with a double-click event:

app.directive('uiSelectCopy', [function(){
    return {
        restrict: 'A',
        scope: {
            $item: '=ngBind'
        },
        link: function ($scope, $elm) {
            $elm.on('dblclick', function(){
                let value = $scope.$item || this.innerText;
                let input = angular.element(`<input value="${value}"/>`);
                angular.element(document.body).append(input);
                input[0].select();
                document.execCommand('copy');
                input.remove();
            });
        }
    }
}])

And then add this directive to your ui-select-match:

<ui-select multiple tagging tagging-label="(custom 'new' label)" ng-model="multipleDemo.colors" theme="bootstrap"
    sortable="true" ng-disabled="disabled" style="width: 300px;" title="Choose a color">
    <ui-select-match placeholder="Select colors...">
        <span ng-bind="$item" ui-select-copy></span>
    </ui-select-match>
    <ui-select-choices repeat="color in availableColors | filter:$select.search">
        {{color}}
    </ui-select-choices>
</ui-select>

You can also change the event to a single click if you want.