相关疑难解决方法(0)

如何在角度指令中对按键事件进行单元测试

我已经创建了一个编辑指令来将html输入包装在一个奇特的框架中.

现在我正在创建一个单元测试来检查一旦输入输入,就会设置表单控制器的脏状态.这是我到目前为止所得到的,但它在第二次预期失败了.

这有什么不对?

提前致谢!

describe('dirty', function () {
    it('false', function () {
        var scope = $rootScope.$new(),
            form = $compile('<form name="form"><edit ng-model="model"></edit></form>')(scope),
            input = form.find('input');
        scope.$digest();
        expect(scope.form.$dirty).toBeFalsy();
        input.triggerHandler('keydown', { which: 65 });
        expect(scope.form.$dirty).toBeTruthy();
    });
});
Run Code Online (Sandbox Code Playgroud)

编辑:

对于所有重要的事情,它归结为这个plunker(没有jQuery)......或者这个使用jQuery

任何的想法?

events jasmine angularjs angularjs-directive

6
推荐指数
1
解决办法
3386
查看次数

Angular指令:可以测试某些字符在按键事件中是否被拒绝?

我一直在构建一个指令,限制用户按下某些无效字符,在这种情况下,使用keypress事件绑定到使用我的指令的输入元素.我一直在尝试测试这个功能,但我不明白如何实现这一点.

我的指示

angular
    .module('gp.rutValidator')
    .directive('gpRutValidator', directive);

  directive.$inject = ['$filter'];

  function directive($filter){
    var ddo = {
      restrict: 'A',
      require: 'ngModel',
      link: linkFn
    };
    return ddo;

    function linkFn(scope, element, attrs, ngModel){

      //valid characters are digits, dash and letter k
      var regexValidKeys = (/[\d\.\-k]/i);

      element.bind('keypress', function(e){

        var key = e.key || String.fromCharCode(e.keyCode);

        if (!regexValidKeys.test(key)) {
          e.preventDefault();
          return false;
        }

      });

    }
  }
Run Code Online (Sandbox Code Playgroud)

我的考试

describe('Angular Rut Validator Directive',validatorDirectiveSpec);

  function validatorDirectiveSpec(){

    //////////////  GLOBALS   ////////////////////////////////
    var scope, element, evt;
    //////////////  BEFORE EACH ////////////////////////////////
    beforeEach(module('gp.rutValidator')); …
Run Code Online (Sandbox Code Playgroud)

javascript testing jasmine angularjs angular-directive

6
推荐指数
1
解决办法
298
查看次数

在输入文本上触发jQuery的keypress事件

关于.trigger()方法,Event对象,which属性,JS字符代码和下面的代码,为什么#example输入不会将char a作为自动写入值?我误解了这个.trigger()方法吗?

<input type="text" name="example" id="example" value="" />

<script>
$(function() {
    var e = jQuery.Event("keydown", { which: 65 });
    $("#example").focus().trigger(e);
});
</script>
Run Code Online (Sandbox Code Playgroud)

jquery triggers keydown which

1
推荐指数
1
解决办法
3674
查看次数