Why $ rootScope. Does $ New not allow a pattern in a directive?

I am building unit testing using Karma and Mocha. Testing my directives and using html2js (it converts htmls to cached strings in $ templateCache). Interestingly, when using $ rootScope. $ New () in my test the html template will not get into the directive. Here is the code:

it('should show a thumb name', function() { inject(function($compile, $rootScope,$controller) { var scope = $rootScope;//.$new() ($new not working. Why?) var linkFn = $compile('<thumb></thumb>'); var element = linkFn(scope); scope.$digest(); // <== needed so that $templateCache will bring the html // (that html2js put in it) console.log(element.html());// correctly returns thumb directive templateUrl content })); 

...

However , if I use scope = $ rootScope. $ new (), element.html () will return an empty string

any ideas?

thank you very much lior

+4
source share
1 answer

According to the docs for $digest ( http://docs.angularjs.org/api/ng . $ RootScope.Scope), this will only handle observers, etc. for current and its children.

This tells me that when you set scope = $rootScope and then $digest you will handle observers, etc. on $rootScope , I think promises will also be allowed here, releasing your templates. When you do scope = $rootScope.$new() and call $digest , I expect that nothing that should come from $rootScope will happen.

So this works if you change scope.$digest() to $rootScope.$digest() or scope.$apply() ?

+2
source

Source: https://habr.com/ru/post/1487143/


All Articles