javascript - Custom ng-enter directive not passing $event from html to the controller -
i using custom directive detects when user presses enter , calls same function called in ngsubmit of surrounding form element.
below demonstration of problem, need access event within controller undefined.
has had issue before? issue? why happening? links refs explanations.
is there better way duplicating method call twice? (secondary)
var app = angular.module('app', []); app.controller('submitctrl', ['$scope', function($scope) { $scope.submitcontent = function(event) { //this looking for. console.log(event); //this undefined. } }]); app.directive('mventer', function() { return function(scope, element, attrs) { element.bind('keydown keypress', function(event) { if (event.which === 13) { scope.$apply(function() { scope.$eval(attrs.mventer); }); event.preventdefault(); } }); }; });
<!doctype html> <html> <head></head> <body ng-app='app'> <div ng-controller="submitctrl"> <textarea mv-enter="submitcontent($event);"></textarea> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> </body> </html>
$eval
method looks $scope.$eval(expr, locals)
, means can use locals
object of $eval
function, key parameter name($event
).
scope.$eval(attrs.mventer, {$event: event});
Comments
Post a Comment