我是新来AngularJS。我想由O'Reilly在AngularJS书中提到的一个演示。我知道,当有一个在形成一个输入框,按下回车键密钥输入里面,会导致两个 NG-点击
和 NG-提交
来触发动作。然而,在我的情况,我只有一个输入字段,即使我不preSS输入的输入域里面的钥匙,我的NG-提交动作称为每次,当我上的复位按钮点击。这里是code。
I am new to AngularJS. I am trying a demo mentioned in AngularJS book by O'Reilly.
I know that when there is one input field in the form, hitting enter key inside that input, would cause both ng-click
and ng-submit
action to be triggered. However, in my case, I have only one input field, even if I do not press enter key inside the input field, my ng-submit action is called everytime when i click on reset button.
Here is code.
<!DOCTYPE html>
<html ng-app="">
<head lang="en">
<meta charset="UTF-8">
<title>Form Submit Action</title>
</head>
<body>
<form ng-submit="requestFunding()"
ng-controller="FormController">
Estimate :
<input ng-model="estimate"/>
<br/>
Recommended :
{{recommended}}
<br/>
<Button>Fund My Start Up</Button>
<Button ng-click="reset()">Reset</Button>
</form>
<script src="Scripts/angular.js"></script>
<script>
function FormController($scope)
{
$scope.estimate = 0;
computeNeeded = function(){
$scope.recommended = $scope.estimate * 10;
};
$scope.$watch('estimate', computeNeeded);
$scope.requestFunding = function()
{
window.alert("Add More Customers First");
};
$scope.reset = function()
{
$scope.estimate = 0;
};
}
</script>
</body>
</html>
有没有逻辑的或概念上的错误?还请指教我就当我使用AngularJS提交和重置表单的正确途径。
Is there any logical or conceptual mistake ? Please also enlighten my on the right way to submit and reset the form when I am using AngularJS.
推荐答案
在stardard HTML,你需要设置键入=复位
来表明这是一个复位键:
In stardard html, you need to set type="reset"
to indicate that this is a reset button:
<button type="reset" ng-click="reset()">Reset</button>
但是,你会看到一个问题,在角这种方法,因为这演示节目。当您点击重置
,输入被设置为空的,而不是0 如您在code指定:
But you would see a problem with this approach in angular, as this DEMO shows. When you click Reset
, the input is set to empty instead of 0 as you specify in your code:
$scope.reset = function() {
$scope.estimate = 0;
};
这样做的原因的问题是,你的 $ scope.reset
第一运行,是的覆盖在默认动作
相关推荐
最新文章