AngularJS jasmine isolateScope() returns undefined
AngularJS jasmine isolateScope() returns undefined
我希望从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'
问题是:为什么我要返回未定义?即使在隔离的范围内什么都没有,它仍然应该是空的{}对象。但无论如何——测试是错误的——它没有显示(实际)包含数据的孤立范围。
我很愚蠢。
但我会回答我自己的问题,因为"愚蠢的人和愚蠢的人一样"——也就是说,可能是一个曾经也会这样做的人(或者将来为我自己)。
问题出现在我的指令正在使用的helpbutton.html
中(我在这个问题中没有显示/提及)。
所以templateUrl
引用的是helpbutton.html
文件,该文件应该被正确地编译为html。
当我查看el.html()
的输出时,我发现它没有被正确地呈现(有一些缺失的标记或其他什么)。
这就是为什么我不能从element
中获得任何scope
的原因。
(不过,如果模板没有正确地呈现为html,那么在日志中出现某种异常会很好)
刚刚遇到了同样的问题,但原因不同。以防其他人也有同样的问题,一旦你以前见过,这可能真的很明显,但现在仍然如此。
如果您使用的是templateUrl,那么在实际调用之前,.sisolateScope()函数将返回undefined$摘要和$httpBackend.flush();
当您直接从指令中使用模板时,它将在您执行$compile(element)(scope)时可用。
如果您刚刚将指令更改为使用templateUrl,或者将其从常规更改为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
相关文章:
- $('#select').val() returns UNDEFINED
- node-mysql connection.query() returns undefined
- jQuery $("body").height() returns undefined
- Meteor cursor.fetch().property returns "undefined"
- JQuery closest() returns undefined
- this.refs.something returns "undefined"
- jquery/js closest().attr() returns undefined
- Javascript ajax returns undefined
- Javascript textContent returns undefined
- tagName returns undefined
- AngularJS jasmine isolateScope() returns undefined
- sqlite3 and node-js returns undefined
- jQuery this.html() returns undefined
- jQuery split() returns undefined
- AngularJS $http.get returns undefined
- $.html() returns undefined?
- jquery returns undefined for data()
- Backbone.js fetch() JSON to model get() returns undefined
- jQuery getScript returns undefined
- Select with ngOptions returns undefined to ngModel