我希望从我的 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());
..
});
-- 在每次测试之前
beforeEach(inject(function($compile, $rootScope, $injector) {
compile = $compile;
scope = $rootScope.$new(); ...
它打印:
'el: [Object Object]'
'Isolated scope: undefined'
问题是:为什么我返回undefined?即使隔离范围内没有任何内容,它仍然应该是空的 {} 对象。但无论如何 - 测试是错误的 - 它没有显示(实际上)其中包含数据的隔离范围。
最佳答案
我傻了。
但会回答我自己的问题,因为“愚蠢就是愚蠢” - 即可能有人曾经会做同样的事情(或者为 future 的我自己)。
问题出在我的指令正在使用的 helpbutton.html 中(我没有在这个问题中显示/提及)。
因此 templateUrl 指的是 helpbutton.html 文件,该文件应该被正确编译为 html。
一旦我查看了 el.html() 的输出,我发现它没有正确呈现(缺少一些标签或其他东西)。
这就是为什么我无法从 element 获得任何 scope 的原因。
(如果模板未正确呈现为 html,在日志中出现某种异常会很好)
关于javascript - AngularJS Jasmine isolateScope() 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26615857/