[英]How to make this error msg not print twice? (how to make ng-keypress not trigger ng-blur)
問題: symc_invalid_pin_name
生成的錯誤消息(具有 /、> 等無效字符)打印了兩次。
我的調查:由於錯誤消息只會在文本框通過在其他地方單擊鼠標而失去焦點時打印一次,但是當我在聚焦文本框時按 Enter 鍵時會打印兩次,我認為這是因為 ng-keypress 觸發了 ng-blur。 當我在尋找解決方案時,我看到了這個。 但我認為答案沒有多大幫助,因為第一個答案只是提供了概念而不是解決方案,第二個答案是使用 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>
咖啡腳本:
$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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.