使用ng-submit
我有一个表单提交回调,即submitFN
。有2个按钮:
我想要的输出是单击重置按钮,输入字段为0.然后点击“Fund my startUp”它会显示一个警告。如何实现这个目标?
问题: 这两个按钮都在触发submitFN。无法重置。
代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js">
</script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('startUpController', function($scope) {
$scope.funding = {
startingEstimate: 0
};
$scope.calculate = function () {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
$scope.submitFN = function () {
window.alert('Go and find more customers.')
}
$scope.resetFN = function () {
$scope.startingEstimate = 0;
}
});
</script>
</head>
<body>
<div ng-app='mainApp'>
<form ng-submit="submitFN()" ng-controller="startUpController">
Starting: <input type="text" ng-change="calculate()" ng-model="funding.startingEstimate"/>
Recommended: {{funding.needed}}
<button>Fund my startup!</button>
<button ng-click="resetFN()">Reset</button>
</form>
</div>
</body>
</html>
我完全是angularJS的新手,任何帮助都非常值得赞赏。提前谢谢。
更新 我试过的一个代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js">
</script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller('startUpController', function($scope) {
$scope.funding = {
startingEstimate: 0
};
$scope.calculate = function () {
$scope.funding.needed = $scope.funding.startingEstimate * 10;
};
$scope.submitFN = function () {
window.alert('Go and find more customers.')
}
$scope.resetFN = function () {
$scope.startingEstimate = 0;
}
});
</script>
</head>
<body>
<div ng-app='mainApp'>
<form ng-submit="submitFN()" ng-controller="startUpController">
Starting: <input type="text" ng-change="calculate()" ng-model="funding.startingEstimate"/>
Recommended: {{funding.needed}}
<button>Fund my startup!</button>
<button type = "button" ng-click="resetFN()">Reset</button>
</form>
</div>
</body>
</html>
答案 0 :(得分:3)
默认情况下,按钮type
是提交的,因此第二个按钮也有按钮类型submit
。如果您不想在该按钮上提交表单,则应明确将该按钮类型设为button
type="button"
你还有错误的resetFN代码,它应该修改一个分配给ng-model的值
$scope.resetFN = function () {
$scope.funding.startingEstimate = 0;
}
答案 1 :(得分:3)
以下更正的代码:
jsonArray1.append(contentsOf: jsonArray2)
答案 2 :(得分:1)
你错过了funding
$scope.resetFN = function () {
$scope.funding.startingEstimate = 0;
}