isolateScope() returns undefined when using templateUrl

前端 未结 5 1531
遥遥无期
遥遥无期 2021-02-07 03:41

I have a directive that I want to unittest, but I\'m running into the issue that I can\'t access my isolated scope. Here\'s the directive:

&l         


        
5条回答
  •  后悔当初
    2021-02-07 04:33

    In my case, I kept running into this in cases where I was trying to isolate a scope on a directive with no isolate scope property.

    function testDirective() {
      return {
        restrict:'EA',
        template:'{{ message }}'
        scope:{} // <-- Removing this made an obvious difference
      };
    }
    function testWithoutIsolateScopeDirective() {
      return {
        restrict:'EA',
        template:'{{ message }}'
      };
    }
    describe('tests pass', function(){
      var compiledElement, isolatedScope, $scope;
      beforeEach(module('test'));
      beforeEach(inject(function ($compile, $rootScope){
        $scope = $rootScope.$new();
        compiledElement = $compile(angular.element('
    '))($scope); isolatedScope = compiledElement.isolateScope(); })); it('element should compile', function () { expect(compiledElement).toBeDefined(); }); it('scope should isolate', function () { expect(isolatedScope).toBeDefined(); }); }); describe('last test fails', function(){ var compiledElement, isolatedScope, $scope; beforeEach(module('test')); beforeEach(inject(function ($compile, $rootScope){ $scope = $rootScope.$new(); compiledElement = $compile(angular.element('
    '))($scope); isolatedScope = compiledElement.isolateScope(); })); it('element should compile', function () { expect(compiledElement).toBeDefined(); }); it('scope should isolate', function () { expect(isolatedScope).toBeDefined(); }); });

提交回复
热议问题