有时给定未定义值的指令属性

时间:2017-11-13 15:04:44

标签: javascript angularjs angular-directive

我在numpad上创建了一个touch指令,当我按下按钮时,我的指令中会得到nullundefined值。这是一个代码:

return {
  restrict: 'E',
  templateUrl: 'partials/template.html',
  require: 'ngModel',
  link: function(scope, elem, attrs, ngModel) {

    elem.bind('touchstart', function(e) {

      var viewValue = ngModel.$viewValue;
      if (e.target.hasAttribute("edit")) {
        var val = viewValue ? viewValue.substring(0, viewValue.length - 1) : '';
        ngModel.$setViewValue(val);
      } else {
        var val = viewValue || '';
        var number = e.target.getAttribute("data-value");
        if (number == null || number.length > 1) {
          var foo = attrs.dataValue;
          $log.info("something wrong with values : " + number + " foo: " + foo);
          return;
        }
        ngModel.$setViewValue(val + number);
      }
      scope.$apply();
    });
  }
};

HTML:

<kbd data-value="1">
  1
  <div class="background">&nbsp;</div>
</kbd>
<kbd data-value="2">
  2
  <div class="background">ABC</div>
</kbd>

控制台输出:

something wrong with values : null foo: undefined

当我在触摸屏上频繁按下按钮时发生这种情况。有人能告诉我出了什么问题吗?

0 个答案:

没有答案