简体   繁体   English

如何使此错误消息不打印两次? (如何使 ng-keypress 不触发 ng-blur)

[英]How to make this error msg not print twice? (how to make ng-keypress not trigger ng-blur)

Problem: The error message generated by symc_invalid_pin_name (having invalid character such as /, >, and so on) is printed twice.问题: symc_invalid_pin_name生成的错误消息(具有 /、> 等无效字符)打印了两次。

My investigation: Since the error message will only be printed once when the textbox loses focus by just clicking mouse somewhere else, but printed twice when I hit enter when focusing on the textbox, I assume it's because ng-keypress triggers ng-blur.我的调查:由于错误消息只会在文本框通过在其他地方单击鼠标而失去焦点时打印一次,但是当我在聚焦文本框时按 Enter 键时会打印两次,我认为这是因为 ng-keypress 触发了 ng-blur。 When I was looking for solution I saw this .当我在寻找解决方案时,我看到了这个 But I don't think the answer helped much since the first answer just provided the concept but not the solution, the second answer was using jQuery instead of AngularJS (I might be wrong because I am new to web developement).但我认为答案没有多大帮助,因为第一个答案只是提供了概念而不是解决方案,第二个答案是使用 jQuery 而不是 AngularJS(我可能是错的,因为我是 Web 开发的新手)。 Also I want to make sure what is causing the problem is the ng-blur being triggered by ng-keypress.此外,我想确定导致问题的原因是 ng-keypress 触发的 ng-blur。

HTML: HTML:

<td id="symbol-pin-name" class="listings-pin-name" ng-click="selectPin(pin)" ng-dblclick="editPinName($event, pin)" ng-class="getUserDefinedNameClass(pin)">
    <div>
      <span ng-click="editPinName($event, pin)"><svg class="icon icon-pencil"><use xlink:href="#icon-pencil"></use></svg></span>
      <span ng-bind="pin.name" ng-show="changedPin.id != pin.id">A26</span>
      <input maxlength="40" type="text" id="edit-pin-name-text-{{pin.id}}" ng-model="editedPinNames[pin.id]" ng-show="changedPin.id == pin.id" ng-blur="pinNameEditBlur($event, pin)" ng-keydown="pinNameChanged($event, pin)" ng-cloak>
    </div>
  </td>

CoffeeScript:咖啡脚本:

$scope.pinNameChanged = (e, p) ->
id = p.id
switch e.keyCode
  when 13
    illegalChars = new RegExp("[$\'\"\\\\<>`\\s]")
    pinToBeChanged = pin for pin in $rootScope.pins when pin.id is id
    if illegalChars.test($scope.editedPinNames[id].trim())
      $rootScope.$emit("GenericError", i18n.symc_invalid_pin_name)
      $scope.changedPin.id = ""
    else
      if $scope.editedPinNames[id].trim() is ""
        matchedPin = $rootScope.masterTerminals.filter ((t) -> t.jedecName is p.jedecName)[0]
        if matchedPin?
          $scope.editedPinNames[id] = if matchedPin.label? then matchedPin.label else ""
        else
          $scope.editedPinNames[id] = ""
      else
        $scope.editedPinNames[id] = $scope.editedPinNames[id].trim()
      $rootScope.$emit('PinPropertyUpdate', [id], name: $scope.editedPinNames[id])
      $rootScope.updateStandardSvg()
      $rootScope.$emit('Redraw')
      $scope.changedPin.id = ""
  when 27
    cancelPinNameEdit()

$scope.pinNameEditBlur = (e, p) -> $scope.pinNameChanged({keyCode: 13}, p)

I am using AngularJS.我正在使用 AngularJS。

you can add你可以加

ng-blur="$event.preventDefault()"

or even for yours pinNameChanged($event, pin) add甚至为您的 pinNameChanged($event, pin) 添加

$event.preventDefault() or $event.stopPropagation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM