var app = angular.module('myApp', []);
app.controller('OrderFormController', function($scope, $http, $filter, $window, $location) {
$scope.names = ['test','test2'];
$scope.retrieveSelectedClass = function(newValue, oldValue, $event) {
alert($event);
}
});
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<meta charset="utf-8"/>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="OrderFormController">
<select ng-model="selectedName" ng-click="event = $event" ng-change="retrieveSelectedClass(selectedName, '{{selectedName}}', event)"
ng-options="(item.name||item) group by item.groupName for item in names"
class="code-helper" id="code-helperId">
<option value="">Select Option</option>
</select>
</body>
</html>
我已了解到该事件未在ng-change中捕获。
所以我将选择更改为:
<select ng-model="selectedName" ng-click="event = $event" ng-change="retrieveSelectedClass(selectedName, '{{selectedName}}', event)"
ng-options="(item.name||item) group by item.groupName for item in names"
class="code-helper" id="code-helperId">
<option value="">Select Option</option>
</select>
我的js到:
$scope.retrieveSelectedClass = function(newValue, oldValue, $event) {
}
但事件仍未定义。
我用它来检测按下ctrl键:
$scope.retrieveSelectedClass = function(newValue, oldValue, $event) {
var windowsEvent = $window;
if (windowsEvent.event.ctrlKey) {
windowsEvent.open('/html/apexEditor.html?name=' + newValue.name, '_blank');
$scope.selectedName = possibleOldValues[0];
return;
}
}
但是这在firefox中失败了,因为在firefox中没有$ windows.event。 我需要检测Ctrl键并在新标签页中打开。 我无法执行on onkeyup或onkeydown,因为该事件将始终被不必要地监听。 有人可以帮忙吗?
答案 0 :(得分:0)
尝试ng-change="retrieveSelectedClass(selectedName, '{{selectedName}}', $event)"
的{{1}}瞬间
和js
ng-change="retrieveSelectedClass(selectedName, '{{selectedName}}', event)"
答案 1 :(得分:0)
您无法将事件传递给ng-change
。
ng-change不是处理更改事件的指令(我知道给定名称会造成混淆),但实际上是在调用ngModelController。$ setViewValue()且值更改时得到通知(因为ng-change向$ viewChangeListeners集合添加一个侦听器)。所以这符合预期。
参考here。
解决方法:
请查看此Demo。
答案 2 :(得分:0)
删除(点击)事件,然后尝试
$scope.retrieveSelectedClass = function(newValue, $event) {
alert($event);
}
});
<meta charset="utf-8"/>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="OrderFormController">
<select ng-model="selectedName" ng-change="retrieveSelectedClass(selectedName, $event)"
ng-options="(item.name||item) group by item.groupName for item in names"
class="code-helper" id="code-helperId">
<option value="">Select Option</option>
</select>
</body>
</html>