Angularjs - $rootScope in directive link function

后端 未结 6 588
庸人自扰
庸人自扰 2021-01-30 10:57

I am asking this question because I am not quite clear on how to think of rootscope as a dependency passed to directives

I have a directive that needs to display some in

6条回答
  •  借酒劲吻你
    2021-01-30 11:04

    Another way is to create a service and throw that service access the $rootScope and other functions. I did it like this because of my environment...

    app.service('myService', function ($rootScope) 
    {
        this.removeItem = function (el) 
        {
           console.log('rootScope: ',$rootScope);
           return true;
        }
    });
    
    
    app.directive('draggable', function($document,myService) 
    {
       return function(scope, element, attr) 
       {
            myService.removeItem({id:1})
       }
    });
    

    If you can, the best way is Mike solution. if not, try my solution.

提交回复
热议问题