How to test on destroy scope

后端 未结 4 864
刺人心
刺人心 2021-02-13 19:02

How to unit testing an $destroy event of a Directive in angularjs?

I have the code in my directive:

scope.$on(\'$destroy\', function () {
    //clean som         


        
4条回答
  •  孤独总比滥情好
    2021-02-13 19:22

    Angular's isolateScope is preferable to using jQuery. You've probably compiled the element in a beforeEach above your test like this:

    myEl = '
    MY EL
    '; scope = $rootScope.$new(); directiveElement = $compile(myEl)(scope); scope.$digest();

    Now in your test you can access the isolateScope and call $destroy:

    var isolated = directiveElement.isolateScope();
    isolated.$destroy();
    

提交回复
热议问题