isolateScope() returns undefined when using templateUrl

前端 未结 5 1595
遥遥无期
遥遥无期 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:41

    I had the same problem. It seems that when calling $compile(element)($scope) in conjunction with using a templateUrl, the digest cycle isn't automatically started. So, you need to set it off manually:

    it('should work', function() {
        var el = $compile('')($scope);
        $scope.$digest();    // Ensure changes are propagated
        console.log('Isolated scope:', el.isolateScope());
        el.isolateScope().save();
        expect($log.log).toHaveBeenCalled();
    });
    

    I'm not sure why the $compile function doesn't do this for you, but it must be some idiosyncracy with the way that templateUrl works, as you don't need to make the call to $scope.$digest() if you use an inline template.

提交回复
热议问题