Đối với câu hỏi đầu tiên bạn thấy plunkr dưới đây.
Nếu bạn vượt qua $ sự kiện thông qua chức năng ng nhấp chuột, bạn có thể truy cập vào sự kiện này trong điều khiển của bạn. Trong ví dụ của tôi, tôi đã kiểm tra xem altKey có đúng là kiểm tra xem phím alt đã được nhấn cùng lúc với lần nhấp hay chưa. Bạn cũng có thể truy cập ctrlKey, shiftKey và nhấn nút chuột. Xem MouseEvent đối tượng ở đây - http://www.w3schools.com/jsref/dom_obj_event.asp
Bộ điều khiển:
angular.module('exampleApp', [])
.controller("ItemCtrl", function($scope){
$scope.items = [
{text: "Bob", id: 1},
{text: "Alice", id: 2},
{text: "Frank", id: 3},
{text: "Lisa", id: 4}
];
$scope.itemList = [];
$scope.addItemIdToList = function(event, item){
if(event.altKey){
if(isItemInList(item)){
removeItemIdFromList(item);
} else {
addItemIdToList(item);
}
} else {
addItemIdAsSingleSelection(item);
}
};
var isItemInList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
return indexOfItem > -1;
}
var removeItemIdFromList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
$scope.itemList.splice(indexOfItem, 1);
};
var addItemIdToList = function(item){
$scope.itemList.push(item.id);
};
var addItemIdAsSingleSelection = function(item){
$scope.itemList = [item.id];
};
})
http://plnkr.co/edit/RAX5oxkTomXxryp0sNNc
Khi logic bắt đầu trở nên phức tạp hơn một chút thì có lẽ nó sẽ là tốt nhất để làm việc này trong một chỉ thị.
Đối với câu hỏi thứ hai những phần cơ bản có thể được nhìn thấy trong các ví dụ sau:
angular.module('exampleApp', [])
.directive('keypressEvents', function ($document, $rootScope) {
return {
restrict: 'E',
link: function() {
console.log('linked');
$document.on('keypress', function(e) {
if(e.altKey){
var s = 223;
var a = 229;
if(e.which == s){
$rootScope.$broadcast("add_next_id");
} else if(e.which == a){
$rootScope.$broadcast("remove_last_id");
}
}
})
}
}
})
.controller("ItemCtrl", function($scope, $rootScope){
$scope.items = [
{text: "Bob", id: 1},
{text: "Alice", id: 2},
{text: "Frank", id: 3},
{text: "Lisa", id: 4}
];
$scope.itemList = [1];
$rootScope.$on('add_next_id', function (evt, obj, key) {
$scope.$apply(function() {
addNextId();
});
});
$rootScope.$on('remove_last_id', function (evt, obj, key) {
$scope.$apply(function() {
removeLastId();
});
});
var addNextId = function(){
var lastId = $scope.itemList[$scope.itemList.length - 1];
if(lastId < $scope.items.length){
$scope.itemList.push(lastId+1);
}
};
var removeLastId = function(){
if($scope.itemList.length > 1){
$scope.itemList.pop();
}
};
$scope.isItemInList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
return indexOfItem > -1;
}
})
http://plnkr.co/edit/PyyjfRMovygeq9qNbzWo
Chúng tôi lắng nghe trên các tài liệu cho máy ép chìa khóa và kiểm tra lại cho altKey. Sau đó, nếu mã khóa là một trong các phím nóng của chúng tôi, chúng tôi sẽ gửi một thông báo tới $ rootScope bằng $ rootScope. $ Broadcast() mà trình điều khiển đang nghe bằng phương thức $ rootScope. $ On().
Trong ví dụ trên, Alt + s sẽ thêm nhiều id và alt + a sẽ xóa các id đó xuống id được chọn ban đầu.