AngularJS - Use attribute directive conditionally

前端 未结 4 1595
走了就别回头了
走了就别回头了 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:02

    Thanks Jason for your suggestion. I took little different approach here. Since I don't want to change the "scope" variable therefore I used "attrs" to check if drag is allowed or not. Following is approach I tool which seems good so far.

    Directive code:

    app.directive('draggable', function () {
        return {
            // A = attribute, E = Element, C = Class and M = HTML Comment
            restrict: 'A',
            replace:true,
            link: function (scope, element, attrs) {
    
                if(attrs.allowdrag =="true")
                {
                    element.draggable({
                    cursor: 'move',
                    helper: 'clone',
                    class:'drag-file'
                    });
                }
    
            }
        }
    });
    

    HTML Code:

    Controller is having implementation of userHasPrivilege().

    Not sure if this is correct way or not. Looking for thoughts.

提交回复
热议问题