在下面的代码片段中,单击 filepath1
和 filepath2
按钮不会触发函数 readCSV()
。
但是,readCSV()
在表单加载时触发。
我希望该功能也能在单击按钮时触发。
如果您能在这里帮助我,我将不胜感激。
谢谢!
<!DOCTYPE html>
<html ng-app="test" ng-controller="homePage">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0" />
<title>TEST</title>
<link rel="stylesheet" href="_css/compiled.css" />
<link rel="stylesheet" href="_css/style-fonts.css" />
<link rel="stylesheet" href="_css/scrollbar/perfect-scrollbar.min.css" />
<script src="_js/libs/angular.min.js"></script>
</head>
<body>
<header class="wrapper headerWrapper mainColorBG">
</header>
<section class="lightBlueBar">
<h2 class="inlineBlock redText"><span class="circularIcon icon-flask"></span> <span class="text">Reports</span></h2>
<div class="searchBar inlineBlock">
<form method="get" ng-submit="readCSV()">
<div ng-controller="submitController">
<label ng-model="filePath" class="fileLabel inlineBlock button grayButton"><span class="icon-cloud-upload"></span><span>Upload</span> <input type="file" ng-model="csvPath"/></label>
<button ng-model="filePath1" ng-click="readCSV()" class="button blueButton"><span class="icon-paper-plane"></span> SUBMIT</button>
<button ng-model="filePath2" ng-click="readCSV()">submit</button>
</div>
</form>
</div>
</section>
</body>
</html>
脚本部分
var test=angular.module("test",[]);
test.controller('submitController',function readCSV($scope, $http){
var csv = "hello";//$scope.csvPath;
$scope.filePath1;
alert(csv);
});
最佳答案
您声明 Controller 的方式是错误的。 Controller 中不需要 readCSV。并且 Controller 始终在加载应用程序时启动。
要调用函数,请执行以下操作
var test=angular.module("test",[]);
test.controller('submitController',function($scope, $http){
$scope.readCSV=function(){
//Do something
};
});
关于javascript - 单击按钮时不会调用函数,但会在表单加载时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32795494/