0

In my code I am creating a reset button to wipe a form clean. with the reset I also want a value that's dependent on that input to also clear. Within the reset() function, $scope.funding.startingEstimate is correctly set to zero, however $scope.funding.needed is not. Furthermore, shouldn't the $watch scope function take care of that already? Anytime the funding.startEstimate is changed (as it is successfully in the $scope.reset function, shouldn't the recommended field also return to zero because the $watch function calls computeNeeded which multiplies funding.startingEstimate by 10 (with 0*10 equaling 0).

Please help.

Below is my code:

app.js

app.controller('StartUpController',["$scope", function($scope){
    $scope.funding = {startingEstimate:0};


    $scope.computeNeeded=function(){
        console.log("test2")
        $scope.funding.needed = $scope.funding.startingEstimate * 10;
    };

    $scope.$watch('funding.startingEstimate', $scope.computeNeeded);

    $scope.requestFunding=function(){
        window.alert("Sorry, please get more customers first");
    };

    $scope.reset=function(){
        console.log("test")
        $scope.funding.startingEstimate = 0;
        $scope.funding.needed = 0;

    };


}]);

<form ng-controller="StartUpController" ng-submit="requestFunding()" ng-reset="reset()">
            Starting: <input ng-model="funding.startingEstimate" > 
            Recommendation:{{funding.needed}}
            <button type="submit" class="btn btn-primary">Request Funding </button>
            <button type="reset" class="btn btn-danger" >Reset</button>
        </form>

1 Answer 1

2

You'll kick yourself when I tell that you neglected to invoke the reset() method in Angular – instead you defined the button's type as reset so you were getting the basic HTML functionality of resetting a form.

I've added the ng-click for you: http://jsfiddle.net/z8edmpp2/

Sign up to request clarification or add additional context in comments.

2 Comments

To add to your answer, I dont believe there exist an Angular directive called ng-reset, so that should be removed altogether.
Agreed. I've never seen a ng-reset directive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.