【发布时间】:2014-01-28 16:17:12
【问题描述】:
给定以下指令
directive('myDirective', function() {
return {
restrict: 'A',
scope: {},
replace: false,
template: '<input ng-focus="onFocus()" type="text" />',
link: function(scope, element, attr) {
scope.onFocus = function() {
console.log('got focus');
};
}
};
});
我已经测试了焦点观察器在浏览器中的工作,但我希望能够在单元测试中触发它。这是我尝试过的方法,但它不起作用。
var element = angular.element('<div my-directive></div>');
$compile(element)($scope);
$scope.$digest();
element.find('input')[0].focus();
我可以看到我通过 find 调用正确地进入了输入框,并且我希望该代码能够触发输入框上的焦点事件,但没有发生任何事情。我在这里错过了什么?
【问题讨论】:
-
我认为你不需要做
element.find('input')元素是输入标签 -
替换设置为假。
element是 div,其中包含的是输入标签。 -
哦,我错过了那部分
-
试试
element.find('input').triggerHandler('focus'); -
$(element.find('input')[0]).triggerHandler('focus');没有这样的运气
标签: javascript jquery angularjs jasmine jasmine-jquery