[英]AngularJS jasmine isolateScope() returns undefined
I'm expecting to get something from isolated scope out of my help-button
directive. 我期待从我的
help-button
指令中获取隔离范围内的内容。
it('should contain proper scope, depending on attributes', function() {
var el = compile('<help-button context-id="1"></help-button>')(scope);
scope.$digest();
console.log("el: " + el);
console.log('Isolated scope: ' + el.isolateScope());
..
});
-- before each test it does - 在每次测试之前
beforeEach(inject(function($compile, $rootScope, $injector) {
compile = $compile;
scope = $rootScope.$new(); ...
it prints: 它打印:
'el: [Object Object]'
'Isolated scope: undefined'
The question is: why I'm getting back undefined ? 问题是:为什么我要回来未定义 ? Even if there is nothing in isolated scope, it still should be empty {} object.
即使在隔离范围内没有任何内容,它仍然应该是空的{}对象。 But anyway - the test is wrong - it does not show the isolated scope which (in real) contains data in there.
但无论如何 - 测试是错误的 - 它没有显示那里(实际上)包含数据的孤立范围。
I'm stupid. 我真笨。
But will reply to my own question because "a stupid is as stupid does" - ie may be one once would do the same (or for myself from the future). 但是会回答我自己的问题,因为“愚蠢是愚蠢的” - 也就是说,可能是一次会做同样的事情(或者对未来的我自己)。
The problem was in my helpbutton.html
which my directive is using (which I did not show/mention in this question). 问题出在我的指令正在使用的
helpbutton.html
中(我在这个问题中没有显示/提及)。
So that templateUrl
were referring to helpbutton.html
file that is supposed to be compiled to html properly. 所以
templateUrl
指的是应该正确编译为html的helpbutton.html
文件。
Once I looked at el.html()
's output I got that it was not properly rendered (there were some missing tag or something). 一旦我看了
el.html()
的输出,我得到它没有正确渲染(有一些遗失的标签或东西)。
Thant's why I could not get any scope
from the element
. Thant是我无法从
element
获得任何scope
的原因。
(though would be nice to have some kind of exception on the log if a template was not rendered properly to html) (如果模板没有正确呈现给html,那么在日志上有一些异常会很好)
Just bumped into the same problem, but with a different cause. 刚碰到同样的问题,但原因不同。 Just in case anyone else has the same issue, and this is probably really obvious once you've seen it before, but still.
以防其他人有同样的问题,一旦你以前见过它,这可能是非常明显的,但仍然如此。
If you are using a templateUrl, then the .isolateScope() function will return undefined until you've actually called .$digest and $httpBackend.flush(); 如果您使用的是templateUrl,那么.isolateScope()函数将返回undefined,直到您实际调用。$ digest和$ httpBackend.flush();
When you use a template directly from the directive, it will be available the moment you do $compile(element)(scope). 当您直接使用指令中的模板时,它会在您执行$ compile(element)(范围)时可用。
This can come as a surprise if you've just changed a directive to use a templateUrl, or changed it from regular to isolateScope. 如果您刚刚更改了指令以使用templateUrl,或者将其从regular更改为isolateScope,则会出现意外情况。
responseHtml = '<div></div>';
$httpBackend.expectGET('template.html').respond(200, responseHtml);
var html = '<widget></widget>';
element = angular.element(html);
var compiled = $compile(element)(scope);
console.log(element.isolateScope()); // undefined
$rootScope.$digest();
console.log(element.isolateScope()); // undefined
$httpBackend.flush();
console.log(element.isolateScope()); // now it will be available
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.