简体   繁体   English

Angular JS ng-click无法正常工作

[英]Angular js ng-click is not working as expected

While working on basic angular examples ng-click is not working as expected 在处理基本的角度示例时,ng-click无法正常工作

the following is my html code : 以下是我的html代码:

<form ng-submit="requestFunding()" ng-controller="StartUpCalculator">
    Starting :
    <input ng-change='ComputeNeeded()' ng-model='funding.StartingEstimate'>Recommedation : {{needed}}
    <button>Fund me</button>
    <button ng-click="reset()">Reset</button>
</form>

Javascript code : JavaScript代码:

function StartUpCalculator($scope)
{
    $scope.funding = {
        StartingEstimate: 0
    };
    ComputeNeeded = function ()
    {
        $scope.needed = $scope.funding.StartingEstimate * 10;
    };

    $scope.requestFunding = function ()
    {
        window.alert("Whoa!");
    };
}

whenever i click Reset(reset()) button it executes requestFunding function 每当我单击Reset(reset())按钮时,它就会执行requestFunding函数

Thre is no $scope.reset() in controller. 控制器中没有$scope.reset() You are triggering the ng-submit by clciking the button. 您通过点击按钮来触发ng-submit

You can change <button> to <button type="button"> so it won't be bound to form submit. 您可以将<button>更改为<button type="button">以便将其绑定到表单提交。 By default any button in a form with no type, and no click handler to preventDefault(), will trigger submit, however type="button" will not. 默认情况下,表单中没有类型的任何按钮,也没有阻止preventDefault()的单击处理程序,都会触发提交,但是type="button"不会。

You also need to change ComputeNeeded to $scope.ComputeNeeded if you want it to work with ng-change 您还需要将ComputeNeeded更改为$scope.ComputeNeeded如果您希望它可以与ng-change

Because on ng-submit you are calling requestFunding . 因为在ng-submit您正在调用requestFunding Add a new button and call requestFunding from there or else remove reset from form tag and place it putside. 添加一个新按钮并从此处调用requestFunding ,否则从表单标签中删除reset并将其放置在放置位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM