[英]AngularJs Custom directive not binding with “textarea”
我為防止輸入鍵制定了自定義指令。 它僅對“輸入”元素有效,但對“文本區域”沒有約束。 這是我的js
function inputFocus() {
return {
restrict: 'E',
require: '?ngModel',
link: function ($scope, elem, attrs) {
elem.bind('keydown', function (event) {
var code = event.keyCode || event.which;
if (code === 13) {
$scope.$apply(function () {
$scope.$eval(attrs.inputFocus);
});
event.preventDefault();
}
});
}
}
}
和:
<textarea class="form-control" name="UserName" maxlength="50" rows="2"
ng-model="UserName" tabindex="2" required>
</textarea>
我通過問您的問題發現了我的錯誤:)我通過“輸入”給指令名稱,以便它與所有輸入元素綁定。 因此,也必須將其與“ textarea”綁定
angular.module('apanelApp').directive('textarea', inputFocus);
感謝所有人的回應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.