admin管理员组文章数量:1390334
I'm using the directive from this answer to run a function when the enter key is pressed in an input field.
How can I pass the value of the input field element.val()
to the function the directive calls? Or pass the input field element
to the function, to clear the value after it's retrieved.
HTML
<input type="text" ng-enter="newField()" />
JS
app.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 13) {
element.val(); // value of input field
scope.$apply(function(){
scope.$eval(attrs.ngEnter); // passed to this function
});
event.preventDefault();
}
});
};
});
I'm using the directive from this answer to run a function when the enter key is pressed in an input field.
How can I pass the value of the input field element.val()
to the function the directive calls? Or pass the input field element
to the function, to clear the value after it's retrieved.
HTML
<input type="text" ng-enter="newField()" />
JS
app.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 13) {
element.val(); // value of input field
scope.$apply(function(){
scope.$eval(attrs.ngEnter); // passed to this function
});
event.preventDefault();
}
});
};
});
Share
Improve this question
edited May 23, 2017 at 12:15
CommunityBot
11 silver badge
asked Jan 22, 2014 at 3:24
nathancahillnathancahill
10.9k11 gold badges55 silver badges93 bronze badges
2 Answers
Reset to default 3You could just do this:
var func = scope.$eval(attrs.ngEnter);
func();
and have the controller take care of the value logic. See the code below. Live demo (click).
Also, I don't remend prefixing your custom directives with ng
. I suggest ing up with your own prefix as a namespace. ng
is for Angular's core. In my examples, I am using my-enter
rather than ng-enter
.
Sample markup:
<input
type="text"
ng-model="foo"
my-enter="myFunc"
placeholder="Type stuff!"
>
<p ng-repeat="val in cachedVals track by $index">{{val}}</p>
JavaScript:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.cachedVals = [];
$scope.foo = '';
$scope.myFunc = function() {
$scope.cachedVals.push($scope.foo);
$scope.foo = '';
};
});
app.directive('myEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 13) {
scope.$apply(function(){
var func = scope.$eval(attrs.myEnter);
func();
});
event.preventDefault();
}
});
};
});
Here's an example with an isolated scope - you don't need to $eval
.
app.directive('myEnter', function() {
return {
scope: {
func: '=myEnter'
},
link: function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 13) {
scope.$apply(function(){
scope.func();
});
event.preventDefault();
}
});
}
};
});
Have you tried ng-submit
? I have created a fiddle to show you how do to what you are after without requiring your own directive.
http://jsfiddle/a6k7g/
Obviously this answer depends on your requirements, and the form plexity.
本文标签: javascriptGet value of input field from Angular directiveStack Overflow
版权声明:本文标题:javascript - Get value of input field from Angular directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744621457a2616052.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论