How to test behavior in the link function of a directive

后端 未结 5 1561
清酒与你
清酒与你 2020-12-23 19:55

In some of my directives, I\'m adding functions to the scope to handle logic specific for the directive. For example:

link: function(scope, element, attrs) {         


        
5条回答
  •  轮回少年
    2020-12-23 20:10

    As stated in a comment reply to @sqlexception. You just really need to get a handle on the directive's scope, which is not hard to do. What you dont want to do is modify your code to satisfy your tests, as it should be the other way around.

    To get the scope of the directive just compile like so:

    var element = $compile().($scope)

    where $scope is declared with $scope = $rootScrope.$new(). Now we can get the isolate scope by doing element.scope()

    I just recently wrote a short blog post about this found here Testing a Linking Function with a plunker to help:

提交回复
热议问题