javascript - 火狐浏览器问题

标签 javascript angularjs events

我已经在 angularJS 中编写了基本代码来打印用户输入。它可以在 Chrome 中运行,但不能在 Firefox 中运行。附上html和js文件。谁能帮我解决这个问题。

var application = angular.module('mainApp', []);
application.controller('app', function($scope){
	$scope.Inputs = [];
	$scope.searchEnter = function() {
		if(event.which == 13 && $scope.Input != "")
		{
			$scope.addInput();
		}
	};
	$scope.addInput = function(){
		$scope.Inputs.push($scope.Input);
		$scope.Input = '';
		};
});	
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>get the input from user and print on page</title>
		 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
		 <script src="controller.js"></script>
		</head>
				<body ng-app="mainApp" ng-controller="app">
					<h2 style="color:blue"> Enter the input value</h2>
					<input type="text" ng-model="Input" ng-keyup="searchEnter()">
					<div id="InputsToPrint">
						<ol>
							<li style="font-size:14px; font-weight:bold; text-transform:capitalize; font-style=italic; color:green" ng-repeat="firstInput in Inputs">
								{{firstInput}}
							</li>
						</ol>
					</div>
				</body>
</html>

在 Firefox 中,输入值不会打印在页面中。

最佳答案

您尚未将 event 对象传递给函数。您应该在调用 searchEnter 方法时传递 $event(事件对象)变量。它在 Chrome 中工作,因为他足够聪明,可以获取事件对象,甚至不需要将其作为参数传递。

ng-keyup="searchEnter($event)"

代码

$scope.searchEnter = function(e) { //firefox needs event to be taken as parameter
    if( !e ) e = window.event; // <-- its needed for IE
    if(e.which == 13 && $scope.Input != "") {
        $scope.addInput();
    }
};

关于javascript - 火狐浏览器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117218/

相关文章:

javascript - 将多维 JSON 键值对转换为 Angular 菜单(无需 Angular 知识)

javascript - 如何从 diff 源获取 iframe html

javascript - 如何在 Angular Material 和 Angular js中仅显示输入字段的最后四位数字?

java - 使用全局 Hook 从条形码扫描仪读取数据

javascript - 如何使用 document.getElementById ("mydiv") 编辑具有相同 ID 的多个 div

javascript - 在 Jquery 中验证表单的输入字段

angularjs - 我可以嵌套 ng-include 吗?

javascript - 如何在版本更改时重新加载资源(HTML/CSS/JS)

javascript - jQuery 一项一项地显示列表项的最佳方式

asp.net - 子Page_Init方法: Event init cannot be found