【发布时间】:2017-12-14 19:16:45
【问题描述】:
我是来自 Knockout 世界的 Angular 新手。为了在表格的最后一个单元格中添加一个新的选项卡,我已经连接到一个按键事件。
我的问题是焦点随后进入 uri 栏中的信息图标,而不是新创建的行中的下一个单元格。我确定修复很简单,我只是找不到解决方案,有人可以帮忙吗?
我正在使用 Angularjs v1.59
var myApp = angular.module('myApp', []);
myApp
.factory('shippingItems', function () {
return {
data: [{
quantity: 1,
width: 1,
height: 1,
length: 1,
weight: 1,
details: 1
},
{
quantity: 2,
width: 2,
height: 2,
length: 2,
weight: 2,
details: 2
},
{
quantity: 3,
width: 3,
height: 3,
length: 3,
weight: 3,
details: 3
}]
};
})
.controller("userCtrl", ['$scope', 'shippingItems', function ($scope, shippingItems) {
$scope.shippingItems = shippingItems.data;
}])
.directive("myTab", ['shippingItems', function (shippingItems) {
return {
link: function (scope, element, attrs) {
scope.index = attrs.indexTracker;
element.bind("keydown keypress", function (event) {
if (scope.index == (shippingItems.data.length-1)) {
if (event.which === 9) {
var newItem = {
quantity: null,
width: null,
height: null,
length: null,
weight: null,
details: null
};
shippingItems.data.push(newItem);
element.next().focus();
}
}
});
}
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="userCtrl">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">lol</h3>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>Quantity</th>
<th>Width x Height x Length (cm)</th>
<th>Weight Per Item</th>
<th>Total Weight</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr class="table-input" ng-repeat="item in shippingItems">
<td><input type="text" ng-model="item.quantity" /></td>
<td><input type="text" ng-model="item.width" /> x <input type="text" ng-model="item.height" /> x <input type="text" ng-model="item.length" /></td>
<td><input type="text" ng-model="item.weight" />kg</td>
<td>{{ (item.weight|number) * (item.quantity|number)}}kg</td>
<td my-tab="checking" index-tracker="{{$index}}"><input type="text" ng-model="item.details" /></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
【问题讨论】:
-
element.bind("keydown keypress",function(){}) 这里必须输入元素,textarea 或 contenteditablediv 否则我们不能绑定按键(在这个例子中你不听 td可能)..还有一个你正在更新工厂值只有你有更新 scope.shippingItems.push(data)
标签: javascript angularjs html