函数未在按钮单击时调用,但在表单加载时调用

时间:2015-09-26 09:11:24

标签: javascript angularjs

在下面的代码段中,点击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);
});

2 个答案:

答案 0 :(得分:1)

尝试:

var test = angular.module("test", []);
test.controller('submitController', function ($scope, $http) {
    $scope.readCSV = function () {
        var csv = "hello";//$scope.csvPath;
        $scope.filePath1;
        alert(csv);
    }
});

答案 1 :(得分:1)

您声明控制器的方式是错误的。控制器中不需要 readCSV 。并且在加载应用程序时始终启动控制器。

你可以调用一个函数来做这样的事情

var test=angular.module("test",[]);
test.controller('submitController',function($scope, $http){
        $scope.readCSV=function(){
          //Do something
        };
});
相关问题