I want to catch the enter key press event on the textbox below. To make it more clear I am using a ng-repeat
to populate the tbody. Here is the HTML:
我想捕获下面文本框上的enter key press事件。为了更清楚地说明,我正在使用一个ng-repeat来填充tbody。HTML:
<td><input type="number" id="closeqty{{$index}}" class="pagination-right closefield"
data-ng-model="closeqtymodel" data-ng-change="change($index)" required placeholder="{{item.closeMeasure}}" /></td>
This is my module:
这是我的模块:
angular.module('components', ['ngResource']);
I am using a resource to populate the table and my controller code is:
我正在使用一个资源填充表格,我的控制器代码是:
function Ajaxy($scope, $resource) {
//controller which has resource to populate the table
}
18 个解决方案
#1
775
You need to add a directive
, like this:
你需要添加一个指令,像这样:
Javascript:
Javascript:
app.directive('myEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.myEnter);
});
event.preventDefault();
}
});
};
});
HTML:
HTML:
<div ng-app="" ng-controller="MainCtrl">
<input type="text" my-enter="doSomething()">
</div>
#2
324
An alternative is to use standard directive ng-keypress="myFunct($event)"
另一种方法是使用标准指令ng-keypress="myFunct($event)"
Then in your controller you can have:
那么在控制器中你可以有:
...
$scope.myFunct = function(keyEvent) {
if (keyEvent.which === 13)
alert('I am an alert');
}
...
#3
144
My simplest approach using just angular build-in directive:
我最简单的方法就是使用角内建指令:
ng-keypress
, ng-keydown
or ng-keyup
.
ng-keypress,ng-keydown或ng-keyup。
Usually, we want add keyboard support for something that already handled by ng-click.
通常,我们希望为已经通过ng-click处理的东西添加键盘支持。
for instance:
例如:
<a ng-click="action()">action</a>
Now, let's add keyboard support.
现在,让我们添加键盘支持。
trigger by enter key:
回车键触发:
<a ng-click="action()"
ng-keydown="$event.keyCode === 13 && action()">action</a>
by space key:
空格键:
<a ng-click="action()"
ng-keydown="$event.keyCode === 32 && action()">action</a>
by space or enter key:
按空格或输入键:
<a ng-click="action()"
ng-keydown="($event.keyCode === 13 || $event.keyCode === 32) && action()">action</a>
if you are in modern browser
如果你在现代浏览器中
<a ng-click="action()"
ng-keydown="[13, 32].includes($event.keyCode) && action()">action</a>
More about keyCode:
keyCode is deprecated but well supported API, you could use $evevt.key in supported browser instead.
See more in https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
关于keyCode的更多信息:不赞成使用keyCode,但得到了良好支持的API,您可以使用$evevt。支持浏览器的键。在https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key看到更多
#4
99
Another simple alternative:
另一个简单的选择:
<input ng-model="edItem" type="text"
ng-keypress="($event.which === 13)?foo(edItem):0"/>
And the ng-ui alternative:
和ng-ui替代:
<input ng-model="edItem" type="text" ui-keypress="{'enter':'foo(edItem)'}"/>
#5
18
Here is what I figured out when I was building an app with a similar requirement, it doesn't require writing a directive and it's relatively simple to tell what it does:
这是我在开发一个应用程序时得出的结论,它有类似的要求,不需要写指令,而且它的功能相对简单:
<input type="text" ng-keypress="($event.charCode==13)?myFunction():return" placeholder="Will Submit on Enter">
#6
15
You can use ng-keydown ="myFunction($event)" as attribute.
可以使用ng-keydown =“myFunction($event)”作为属性。
<input ng-keydown="myFunction($event)" type="number">
myFunction(event) {
if(event.keyCode == 13) { // '13' is the key code for enter
// do what you want to do when 'enter' is pressed :)
}
}
#7
4
html
html
<textarea id="messageTxt"
rows="5"
placeholder="Escriba su mensaje"
ng-keypress="keyPressed($event)"
ng-model="smsData.mensaje">
</textarea>
controller.js
controller.js
$scope.keyPressed = function (keyEvent) {
if (keyEvent.keyCode == 13) {
alert('presiono enter');
console.log('presiono enter');
}
};
#8
3
Trying
尝试
ng-keypress="console.log($event)"
ng-keypress="alert(123)"
did nothing for me.
对我来说什么也没做。
Strangley the sample at https://docs.angularjs.org/api/ng/directive/ngKeypress, which does ng-keypress="count = count + 1", works.
在https://docs.angularjs.org/api/ng/directive/ngKeypress上的示例中,可以使用ng-keypress=“count = count + 1”。
I found an alternate solution, which has pressing Enter invoke the button's ng-click.
我找到了另一种解决方案,即按Enter invoke按钮的ng-click。
<input ng-model="..." onkeypress="if (event.which==13) document.getElementById('button').click()"/>
<button id="button" ng-click="doSomething()">Done</button>
#9
2
You can also apply it to a controller on a parent element. This example can be used to highlight a row in a table by pressing up/down arrow keys.
还可以将其应用于父元素上的控制器。这个示例可以通过按上/下箭头键来突出显示表中的一行。
app.controller('tableCtrl', [ '$scope', '$element', function($scope, $element) {
$scope.index = 0; // row index
$scope.data = []; // array of items
$scope.keypress = function(offset) {
console.log('keypress', offset);
var i = $scope.index + offset;
if (i < 0) { i = $scope.data.length - 1; }
if (i >= $scope.data.length) { i = 0; }
};
$element.bind("keydown keypress", function (event) {
console.log('keypress', event, event.which);
if(event.which === 38) { // up
$scope.keypress(-1);
} else if (event.which === 40) { // down
$scope.keypress(1);
} else {
return;
}
event.preventDefault();
});
}]);
<table class="table table-striped" ng-controller="tableCtrl">
<thead>
<tr>
<th ng-repeat="(key, value) in data[0]">{{key}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in data track by $index" ng-click="draw($index)" ng-class="$index == index ? 'info' : ''">
<td ng-repeat="(key, value) in row">{{value}}</td>
</tr>
</tbody>
</table>
#10
2
This is an extension on the answer from EpokK.
这是EpokK的一个扩展。
I had the same problem of having to call a scope function when enter is pushed on an input field. However I also wanted to pass the value of the input field to the function specified. This is my solution:
我遇到了同样的问题,当输入被推入一个输入字段时,我必须调用一个范围函数。不过,我也想将输入字段的值传递给指定的函数。这是我的解决方案:
app.directive('ltaEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
// Create closure with proper command
var fn = function(command) {
var cmd = command;
return function() {
scope.$eval(cmd);
};
}(attrs.ltaEnter.replace('()', '("'+ event.target.value +'")' ));
// Apply function
scope.$apply(fn);
event.preventDefault();
}
});
};
});
});
The use in HTML is as follows:
HTML的用途如下:
<input type="text" name="itemname" lta-enter="add()" placeholder="Add item"/>
Kudos to EpokK for his answer.
对EpokK的回答表示敬意。
#11
1
What about this?:
这是什么?:
<form ng-submit="chat.sendMessage()">
<input type="text" />
<button type="submit">
</form>
Now when you push enter key after write something in your input, the form know how to handle it.
现在,当您在输入中写入内容后按enter键时,表单知道如何处理它。
#12
1
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Informe your name:<input type="text" ng-model="pergunta" ng-keypress="pressionou_enter($event)" ></input>
<button ng-click="chamar()">submit</button>
<h1>{{resposta}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
//create a service mitsuplik
app.service('mitsuplik', function() {
this.myFunc = function (parametro) {
var tmp = "";
for (var x=0;x<parametro.length;x++)
{
tmp = parametro.substring(x,x+1) + tmp;
}
return tmp;
}
});
//Calling our service
app.controller('myCtrl', function($scope, mitsuplik) {
$scope.chamar = function() {
$scope.resposta = mitsuplik.myFunc($scope.pergunta);
};
//if mitsuplik press [ENTER], execute too
$scope.pressionou_enter = function(keyEvent) {
if (keyEvent.which === 13)
{
$scope.chamar();
}
}
});
</script>
</body>
</html>
#13
0
Some example of code that I did for my project. Basically you add tags to your entity. Imagine you have input text, on entering Tag name you get drop-down menu with preloaded tags to choose from, you navigate with arrows and select with Enter:
我为我的项目做的一些代码示例。基本上,你向你的实体添加标签。假设你有输入文本,在输入标签名时,你会得到一个下拉菜单,里面有预加载的标签可以选择,你会使用箭头导航,然后选择Enter:
HTML + AngularJS v1.2.0-rc.3
HTML + AngularJS v1.2.0-rc.3
<div>
<form ng-submit="addTag(newTag)">
<input id="newTag" ng-model="newTag" type="text" class="form-control" placeholder="Enter new tag"
style="padding-left: 10px; width: 700px; height: 33px; margin-top: 10px; margin-bottom: 3px;" autofocus
data-toggle="dropdown"
ng-change="preloadTags()"
ng-keydown="navigateTags($event)">
<div ng-show="preloadedTags.length > 0">
<nav class="dropdown">
<div class="dropdown-menu preloadedTagPanel">
<div ng-repeat="preloadedTag in preloadedTags"
class="preloadedTagItemPanel"
ng-class="preloadedTag.activeTag ? 'preloadedTagItemPanelActive' : '' "
ng-click="selectTag(preloadedTag)"
tabindex="{{ $index }}">
<a class="preloadedTagItem"
ng-class="preloadedTag.activeTag ? 'preloadedTagItemActive' : '' "
ng-click="selectTag(preloadedTag)">{{ preloadedTag.label }}</a>
</div>
</div>
</nav>
</div>
</form>
</div>
Controller.js
Controller.js
$scope.preloadTags = function () {
var newTag = $scope.newTag;
if (newTag && newTag.trim()) {
newTag = newTag.trim().toLowerCase();
$http(
{
method: 'GET',
url: 'api/tag/gettags',
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
params: {'term': newTag}
}
)
.success(function (result) {
$scope.preloadedTags = result;
$scope.preloadedTagsIndex = -1;
}
)
.error(function (data, status, headers, config) {
}
);
} else {
$scope.preloadedTags = {};
$scope.preloadedTagsIndex = -1;
}
};
function checkIndex(index) {
if (index > $scope.preloadedTags.length - 1) {
return 0;
}
if (index < 0) {
return $scope.preloadedTags.length - 1;
}
return index;
}
function removeAllActiveTags() {
for (var x = 0; x < $scope.preloadedTags.length; x++) {
if ($scope.preloadedTags[x].activeTag) {
$scope.preloadedTags[x].activeTag = false;
}
}
}
$scope.navigateTags = function ($event) {
if (!$scope.newTag || $scope.preloadedTags.length == 0) {
return;
}
if ($event.keyCode == 40) { // down
removeAllActiveTags();
$scope.preloadedTagsIndex = checkIndex($scope.preloadedTagsIndex + 1);
$scope.preloadedTags[$scope.preloadedTagsIndex].activeTag = true;
} else if ($event.keyCode == 38) { // up
removeAllActiveTags();
$scope.preloadedTagsIndex = checkIndex($scope.preloadedTagsIndex - 1);
$scope.preloadedTags[$scope.preloadedTagsIndex].activeTag = true;
} else if ($event.keyCode == 13) { // enter
removeAllActiveTags();
$scope.selectTag($scope.preloadedTags[$scope.preloadedTagsIndex]);
}
};
$scope.selectTag = function (preloadedTag) {
$scope.addTag(preloadedTag.label);
};
CSS + Bootstrap v2.3.2
CSS +引导v2.3.2
.preloadedTagPanel {
background-color: #FFFFFF;
display: block;
min-width: 250px;
max-width: 700px;
border: 1px solid #666666;
padding-top: 0;
border-radius: 0;
}
.preloadedTagItemPanel {
background-color: #FFFFFF;
border-bottom: 1px solid #666666;
cursor: pointer;
}
.preloadedTagItemPanel:hover {
background-color: #666666;
}
.preloadedTagItemPanelActive {
background-color: #666666;
}
.preloadedTagItem {
display: inline-block;
text-decoration: none;
margin-left: 5px;
margin-right: 5px;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 20px;
padding-right: 10px;
color: #666666 !important;
font-size: 11px;
}
.preloadedTagItem:hover {
background-color: #666666;
}
.preloadedTagItemActive {
background-color: #666666;
color: #FFFFFF !important;
}
.dropdown .preloadedTagItemPanel:last-child {
border-bottom: 0;
}
#14
0
I'm a bit late .. but i found a simpler solution using auto-focus
.. This could be useful for buttons or other when popping a dialog
:
我有点晚了。但是我发现了一种更简单的方法:自动对焦。当弹出一个对话框时,这可能对按钮或其他工具有用:
<button auto-focus ng-click="func()">ok</button>
<按钮自动对焦ng-click = " func()"> 好> < /按钮
That should be fine if you want to press the button on
Space or Enter clicks .
如果您想在空格上按下按钮或输入单击,应该没问题。
#15
0
here's my directive:
这是我的指令:
mainApp.directive('number', function () {
return {
link: function (scope, el, attr) {
el.bind("keydown keypress", function (event) {
//ignore all characters that are not numbers, except backspace, delete, left arrow and right arrow
if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) {
event.preventDefault();
}
});
}
};
});
usage:
用法:
<input number />
#16
0
you can use ng-keydown , ng-keyup , ng-press such as this .
你可以像这样使用ng-keydown, ng-keyup, ng-press。
to triger a function :
对triger函数:
<input type="text" ng-keypress="function()"/>
or if you have one condion such as when he press escape (27 is the key code for escape)
或者如果您有一个条件,例如当他按escape时(27是escape的关键代码)
<form ng-keydown=" event.which=== 27?cancelSplit():0">
....
</form>
#17
0
I think using document.bind is a bit more elegant
我认为使用文档。bind更为优雅
constructor($scope, $document) {
var that = this;
$document.bind("keydown", function(event) {
$scope.$apply(function(){
that.handleKeyDown(event);
});
});
}
To get document to the controller constructor:
向控制器构造函数获取文档:
controller: ['$scope', '$document', MyCtrl]
#18
0
How about just
只是如何
<input (keyup.enter)="doWhatYouWant()">
#1
775
You need to add a directive
, like this:
你需要添加一个指令,像这样:
Javascript:
Javascript:
app.directive('myEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.myEnter);
});
event.preventDefault();
}
});
};
});
HTML:
HTML:
<div ng-app="" ng-controller="MainCtrl">
<input type="text" my-enter="doSomething()">
</div>
#2
324
An alternative is to use standard directive ng-keypress="myFunct($event)"
另一种方法是使用标准指令ng-keypress="myFunct($event)"
Then in your controller you can have:
那么在控制器中你可以有:
...
$scope.myFunct = function(keyEvent) {
if (keyEvent.which === 13)
alert('I am an alert');
}
...
#3
144
My simplest approach using just angular build-in directive:
我最简单的方法就是使用角内建指令:
ng-keypress
, ng-keydown
or ng-keyup
.
ng-keypress,ng-keydown或ng-keyup。
Usually, we want add keyboard support for something that already handled by ng-click.
通常,我们希望为已经通过ng-click处理的东西添加键盘支持。
for instance:
例如:
<a ng-click="action()">action</a>
Now, let's add keyboard support.
现在,让我们添加键盘支持。
trigger by enter key:
回车键触发:
<a ng-click="action()"
ng-keydown="$event.keyCode === 13 && action()">action</a>
by space key:
空格键:
<a ng-click="action()"
ng-keydown="$event.keyCode === 32 && action()">action</a>
by space or enter key:
按空格或输入键:
<a ng-click="action()"
ng-keydown="($event.keyCode === 13 || $event.keyCode === 32) && action()">action</a>
if you are in modern browser
如果你在现代浏览器中
<a ng-click="action()"
ng-keydown="[13, 32].includes($event.keyCode) && action()">action</a>
More about keyCode:
keyCode is deprecated but well supported API, you could use $evevt.key in supported browser instead.
See more in https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
关于keyCode的更多信息:不赞成使用keyCode,但得到了良好支持的API,您可以使用$evevt。支持浏览器的键。在https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key看到更多
#4
99
Another simple alternative:
另一个简单的选择:
<input ng-model="edItem" type="text"
ng-keypress="($event.which === 13)?foo(edItem):0"/>
And the ng-ui alternative:
和ng-ui替代:
<input ng-model="edItem" type="text" ui-keypress="{'enter':'foo(edItem)'}"/>
#5
18
Here is what I figured out when I was building an app with a similar requirement, it doesn't require writing a directive and it's relatively simple to tell what it does:
这是我在开发一个应用程序时得出的结论,它有类似的要求,不需要写指令,而且它的功能相对简单:
<input type="text" ng-keypress="($event.charCode==13)?myFunction():return" placeholder="Will Submit on Enter">
#6
15
You can use ng-keydown ="myFunction($event)" as attribute.
可以使用ng-keydown =“myFunction($event)”作为属性。
<input ng-keydown="myFunction($event)" type="number">
myFunction(event) {
if(event.keyCode == 13) { // '13' is the key code for enter
// do what you want to do when 'enter' is pressed :)
}
}
#7
4
html
html
<textarea id="messageTxt"
rows="5"
placeholder="Escriba su mensaje"
ng-keypress="keyPressed($event)"
ng-model="smsData.mensaje">
</textarea>
controller.js
controller.js
$scope.keyPressed = function (keyEvent) {
if (keyEvent.keyCode == 13) {
alert('presiono enter');
console.log('presiono enter');
}
};
#8
3
Trying
尝试
ng-keypress="console.log($event)"
ng-keypress="alert(123)"
did nothing for me.
对我来说什么也没做。
Strangley the sample at https://docs.angularjs.org/api/ng/directive/ngKeypress, which does ng-keypress="count = count + 1", works.
在https://docs.angularjs.org/api/ng/directive/ngKeypress上的示例中,可以使用ng-keypress=“count = count + 1”。
I found an alternate solution, which has pressing Enter invoke the button's ng-click.
我找到了另一种解决方案,即按Enter invoke按钮的ng-click。
<input ng-model="..." onkeypress="if (event.which==13) document.getElementById('button').click()"/>
<button id="button" ng-click="doSomething()">Done</button>
#9
2
You can also apply it to a controller on a parent element. This example can be used to highlight a row in a table by pressing up/down arrow keys.
还可以将其应用于父元素上的控制器。这个示例可以通过按上/下箭头键来突出显示表中的一行。
app.controller('tableCtrl', [ '$scope', '$element', function($scope, $element) {
$scope.index = 0; // row index
$scope.data = []; // array of items
$scope.keypress = function(offset) {
console.log('keypress', offset);
var i = $scope.index + offset;
if (i < 0) { i = $scope.data.length - 1; }
if (i >= $scope.data.length) { i = 0; }
};
$element.bind("keydown keypress", function (event) {
console.log('keypress', event, event.which);
if(event.which === 38) { // up
$scope.keypress(-1);
} else if (event.which === 40) { // down
$scope.keypress(1);
} else {
return;
}
event.preventDefault();
});
}]);
<table class="table table-striped" ng-controller="tableCtrl">
<thead>
<tr>
<th ng-repeat="(key, value) in data[0]">{{key}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in data track by $index" ng-click="draw($index)" ng-class="$index == index ? 'info' : ''">
<td ng-repeat="(key, value) in row">{{value}}</td>
</tr>
</tbody>
</table>
#10
2
This is an extension on the answer from EpokK.
这是EpokK的一个扩展。
I had the same problem of having to call a scope function when enter is pushed on an input field. However I also wanted to pass the value of the input field to the function specified. This is my solution:
我遇到了同样的问题,当输入被推入一个输入字段时,我必须调用一个范围函数。不过,我也想将输入字段的值传递给指定的函数。这是我的解决方案:
app.directive('ltaEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
// Create closure with proper command
var fn = function(command) {
var cmd = command;
return function() {
scope.$eval(cmd);
};
}(attrs.ltaEnter.replace('()', '("'+ event.target.value +'")' ));
// Apply function
scope.$apply(fn);
event.preventDefault();
}
});
};
});
});
The use in HTML is as follows:
HTML的用途如下:
<input type="text" name="itemname" lta-enter="add()" placeholder="Add item"/>
Kudos to EpokK for his answer.
对EpokK的回答表示敬意。
#11
1
What about this?:
这是什么?:
<form ng-submit="chat.sendMessage()">
<input type="text" />
<button type="submit">
</form>
Now when you push enter key after write something in your input, the form know how to handle it.
现在,当您在输入中写入内容后按enter键时,表单知道如何处理它。
#12
1
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
Informe your name:<input type="text" ng-model="pergunta" ng-keypress="pressionou_enter($event)" ></input>
<button ng-click="chamar()">submit</button>
<h1>{{resposta}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
//create a service mitsuplik
app.service('mitsuplik', function() {
this.myFunc = function (parametro) {
var tmp = "";
for (var x=0;x<parametro.length;x++)
{
tmp = parametro.substring(x,x+1) + tmp;
}
return tmp;
}
});
//Calling our service
app.controller('myCtrl', function($scope, mitsuplik) {
$scope.chamar = function() {
$scope.resposta = mitsuplik.myFunc($scope.pergunta);
};
//if mitsuplik press [ENTER], execute too
$scope.pressionou_enter = function(keyEvent) {
if (keyEvent.which === 13)
{
$scope.chamar();
}
}
});
</script>
</body>
</html>
#13
0
Some example of code that I did for my project. Basically you add tags to your entity. Imagine you have input text, on entering Tag name you get drop-down menu with preloaded tags to choose from, you navigate with arrows and select with Enter:
我为我的项目做的一些代码示例。基本上,你向你的实体添加标签。假设你有输入文本,在输入标签名时,你会得到一个下拉菜单,里面有预加载的标签可以选择,你会使用箭头导航,然后选择Enter:
HTML + AngularJS v1.2.0-rc.3
HTML + AngularJS v1.2.0-rc.3
<div>
<form ng-submit="addTag(newTag)">
<input id="newTag" ng-model="newTag" type="text" class="form-control" placeholder="Enter new tag"
style="padding-left: 10px; width: 700px; height: 33px; margin-top: 10px; margin-bottom: 3px;" autofocus
data-toggle="dropdown"
ng-change="preloadTags()"
ng-keydown="navigateTags($event)">
<div ng-show="preloadedTags.length > 0">
<nav class="dropdown">
<div class="dropdown-menu preloadedTagPanel">
<div ng-repeat="preloadedTag in preloadedTags"
class="preloadedTagItemPanel"
ng-class="preloadedTag.activeTag ? 'preloadedTagItemPanelActive' : '' "
ng-click="selectTag(preloadedTag)"
tabindex="{{ $index }}">
<a class="preloadedTagItem"
ng-class="preloadedTag.activeTag ? 'preloadedTagItemActive' : '' "
ng-click="selectTag(preloadedTag)">{{ preloadedTag.label }}</a>
</div>
</div>
</nav>
</div>
</form>
</div>
Controller.js
Controller.js
$scope.preloadTags = function () {
var newTag = $scope.newTag;
if (newTag && newTag.trim()) {
newTag = newTag.trim().toLowerCase();
$http(
{
method: 'GET',
url: 'api/tag/gettags',
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
params: {'term': newTag}
}
)
.success(function (result) {
$scope.preloadedTags = result;
$scope.preloadedTagsIndex = -1;
}
)
.error(function (data, status, headers, config) {
}
);
} else {
$scope.preloadedTags = {};
$scope.preloadedTagsIndex = -1;
}
};
function checkIndex(index) {
if (index > $scope.preloadedTags.length - 1) {
return 0;
}
if (index < 0) {
return $scope.preloadedTags.length - 1;
}
return index;
}
function removeAllActiveTags() {
for (var x = 0; x < $scope.preloadedTags.length; x++) {
if ($scope.preloadedTags[x].activeTag) {
$scope.preloadedTags[x].activeTag = false;
}
}
}
$scope.navigateTags = function ($event) {
if (!$scope.newTag || $scope.preloadedTags.length == 0) {
return;
}
if ($event.keyCode == 40) { // down
removeAllActiveTags();
$scope.preloadedTagsIndex = checkIndex($scope.preloadedTagsIndex + 1);
$scope.preloadedTags[$scope.preloadedTagsIndex].activeTag = true;
} else if ($event.keyCode == 38) { // up
removeAllActiveTags();
$scope.preloadedTagsIndex = checkIndex($scope.preloadedTagsIndex - 1);
$scope.preloadedTags[$scope.preloadedTagsIndex].activeTag = true;
} else if ($event.keyCode == 13) { // enter
removeAllActiveTags();
$scope.selectTag($scope.preloadedTags[$scope.preloadedTagsIndex]);
}
};
$scope.selectTag = function (preloadedTag) {
$scope.addTag(preloadedTag.label);
};
CSS + Bootstrap v2.3.2
CSS +引导v2.3.2
.preloadedTagPanel {
background-color: #FFFFFF;
display: block;
min-width: 250px;
max-width: 700px;
border: 1px solid #666666;
padding-top: 0;
border-radius: 0;
}
.preloadedTagItemPanel {
background-color: #FFFFFF;
border-bottom: 1px solid #666666;
cursor: pointer;
}
.preloadedTagItemPanel:hover {
background-color: #666666;
}
.preloadedTagItemPanelActive {
background-color: #666666;
}
.preloadedTagItem {
display: inline-block;
text-decoration: none;
margin-left: 5px;
margin-right: 5px;
padding-top: 5px;
padding-bottom: 5px;
padding-left: 20px;
padding-right: 10px;
color: #666666 !important;
font-size: 11px;
}
.preloadedTagItem:hover {
background-color: #666666;
}
.preloadedTagItemActive {
background-color: #666666;
color: #FFFFFF !important;
}
.dropdown .preloadedTagItemPanel:last-child {
border-bottom: 0;
}
#14
0
I'm a bit late .. but i found a simpler solution using auto-focus
.. This could be useful for buttons or other when popping a dialog
:
我有点晚了。但是我发现了一种更简单的方法:自动对焦。当弹出一个对话框时,这可能对按钮或其他工具有用:
<button auto-focus ng-click="func()">ok</button>
<按钮自动对焦ng-click = " func()"> 好> < /按钮
That should be fine if you want to press the button on
Space or Enter clicks .
如果您想在空格上按下按钮或输入单击,应该没问题。
#15
0
here's my directive:
这是我的指令:
mainApp.directive('number', function () {
return {
link: function (scope, el, attr) {
el.bind("keydown keypress", function (event) {
//ignore all characters that are not numbers, except backspace, delete, left arrow and right arrow
if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) {
event.preventDefault();
}
});
}
};
});
usage:
用法:
<input number />
#16
0
you can use ng-keydown , ng-keyup , ng-press such as this .
你可以像这样使用ng-keydown, ng-keyup, ng-press。
to triger a function :
对triger函数:
<input type="text" ng-keypress="function()"/>
or if you have one condion such as when he press escape (27 is the key code for escape)
或者如果您有一个条件,例如当他按escape时(27是escape的关键代码)
<form ng-keydown=" event.which=== 27?cancelSplit():0">
....
</form>
#17
0
I think using document.bind is a bit more elegant
我认为使用文档。bind更为优雅
constructor($scope, $document) {
var that = this;
$document.bind("keydown", function(event) {
$scope.$apply(function(){
that.handleKeyDown(event);
});
});
}
To get document to the controller constructor:
向控制器构造函数获取文档:
controller: ['$scope', '$document', MyCtrl]
#18
0
How about just
只是如何
<input (keyup.enter)="doWhatYouWant()">