AngularJS - Use attribute directive conditionally

前端 未结 4 1589
走了就别回头了
走了就别回头了 2020-12-23 13:48

I am using \"draggable\" directive to support image dragging. However, as per the role of the user, I need to disable image dragging for certain groups of users. I have used

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 14:00

    I used a different approach as the previous examples didn't work for me. Perhaps it has to do with using custom directives? Perhaps someone can clear that up.

    In my particular example, I'm using ui-grid, but not all ui-grids should use pagination. I pass in a "paginated" attribute and then $compile the directive based on true/false. Seems pretty brutish but hopefully it can push people in a positive direction.

    HTML

    
    

    Directive

    angular
        .module('app.directives')
        .directive('syncGrid', ['$compile', SyncGrid]);
    
    function SyncGrid($compile){
        var nonPaginatedTemplate = '' +
            '
    ' + '
    ' + '
    '; var paginatedTemplate = '' + '
    ' + '
    ' + '
    '; return { link: link, restrict: 'E', replace: true }; function link(scope, element, attrs) { var isPaginated = attrs['paginated']; var template = isPaginated ? paginatedTemplate : nonPaginatedTemplate; var linkFn = $compile(template); var content = linkFn(scope); element.append(content); // Continue with ui-grid initialization code // ... } }

提交回复
热议问题