gri*_*ett 6 javascript jquery jasmine jasmine-jquery angularjs
给出以下指令
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');
};
}
};
});
Run Code Online (Sandbox Code Playgroud)
我已经测试了焦点观察器在浏览器中工作,但我希望能够在单元测试中触发它.这是我尝试过的,但它不起作用.
var element = angular.element('<div my-directive></div>');
$compile(element)($scope);
$scope.$digest();
element.find('input')[0].focus();
Run Code Online (Sandbox Code Playgroud)
我可以看到我正确地使用find调用进入输入框,我希望代码在输入框上触发焦点事件,但什么也没发生.我在这里错过了什么?
dco*_*ith 17
当尝试触发角度元素上的事件时,应该使用triggerHandler()函数,它实际上只是jqLite函数而不是角度函数,然后将事件作为字符串参数传递,如下所示.
element.find('input').triggerHandler('focus');
Run Code Online (Sandbox Code Playgroud)
要在角度元素上执行任何其他功能,请阅读此处的文档,它会显示角度元素可用的功能列表
| 归档时间: |
|
| 查看次数: |
6988 次 |
| 最近记录: |