简体   繁体   中英

AngularJs Custom directive not binding with “textarea”

I make a custom directives for prevent enter key. It work fine for only "input" element, but it's not binding on "textarea". here is my 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();
                }
            });
        }
    }
}

And:

<textarea class="form-control" name="UserName" maxlength="50" rows="2" 
          ng-model="UserName" tabindex="2" required>
</textarea>

I found my mistake by asking your question :) I give directives name by "input" so that it bind with all input elements. So, have to bind this also with "textarea" like

angular.module('apanelApp').directive('textarea', inputFocus);

Thanks to all for response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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