简体   繁体   中英

Angular js ng-click is not working as expected

While working on basic angular examples ng-click is not working as expected

the following is my html code :

<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 :

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

Thre is no $scope.reset() in controller. You are triggering the ng-submit by clciking the button.

You can change <button> to <button type="button"> so it won't be bound to form submit. By default any button in a form with no type, and no click handler to preventDefault(), will trigger submit, however type="button" will not.

You also need to change ComputeNeeded to $scope.ComputeNeeded if you want it to work with ng-change

Because on ng-submit you are calling requestFunding . Add a new button and call requestFunding from there or else remove reset from form tag and place it putside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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