通过书中的示例AngularJS,为什么点击Reset
按钮会产生警告sorry, please get more customers.
?
我只想在按下alert
按钮时发生Fund my startup!
。
<html ng-app>
<body>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js">
</script>
<form ng-submit="requestFunding()" ng-controller="StartUpController">
Starting: <input ng-change="computeNeeded()" ng-model="startingEstimate">
Recommendation: {{needed}}
<button>Fund my startup!</button>
<button ng-click="reset()">Reset</button>
</form>
<script>
function StartUpController($scope) {
$scope.computeNeeded = function() {
$scope.needed= $scope.startingEstimate * 10;
};
$scope.requestFunding = function() {
window.alert("sorry, please get more customers.");
};
$scope.reset = function() {
$scope.startingEstimate = 0;
};
}
</script>
</body>
</html>
答案 0 :(得分:2)
您需要阻止默认按钮点击行为(即提交)
以下是单向的。 (你也可以为此目的创建一个指令。)
模板:
<button ng-click="reset($event)">Reset</button>
的javascript:
$scope.reset = function( event ) {
event.preventDefault();
$scope.startingEstimate = 0;
};
<button type="button" ng-click="reset()">Reset</button>
这可以通过覆盖button元素的type属性来实现 这恰好是'提交'。