问题描述
问题:由symc_invalid_pin_name
生成的错误消息(具有无效字符,例如/,>等)被打印两次。
我的调查:由于错误消息仅在通过单击鼠标在其他位置的文本框失去焦点时才会打印一次,而在我专注于文本框时按回车键时只会打印两次,因此我认为这是因为ng-keypress触发ng-模糊。在寻找解决方案时,我看到了this。但是我认为答案没有多大作用,因为第一个答案只是提供了概念但没有提供解决方案,第二个答案是使用jQuery而不是AngularJS(我可能是错的,因为我是Web开发的新手)。另外,我还要确定导致问题的原因是ng-keypress触发了ng-blur。
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)
我正在使用AngularJS。
解决方法
您可以添加
ng-blur="$event.preventDefault()"
甚至您的pinNameChanged($ event,pin)添加
$event.preventDefault() or $event.stopPropagation()