如何使此错误消息不打印两次?(如何使按下ng键不会触发ng模糊)



问题:symc_invalid_pin_name生成的错误消息(具有无效字符,如/、>等(打印两次。

我的调查:由于当文本框在其他地方点击鼠标失去焦点时,错误消息只会打印一次,而当我在文本框上点击回车键时,错误信息会打印两次,我认为这是因为按下ng键会触发ng模糊。当我在寻找解决方案时,我看到了这个。但我不认为这个答案有多大帮助,因为第一个答案只是提供了概念,而不是解决方案,第二个答案是使用jQuery而不是AngularJS(我可能错了,因为我是web开发的新手(。此外,我想确保问题的原因是按下ng键触发的ng模糊。

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()

最新更新