0

I am trying to make a form which displays after clicking the button "Click to enter Observations". The same form should appear when the "Next Observation" Button is clicked. This should happen in a loop until I click the "Finish" Button.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<form>
Vendor Name: <input type="text"> <br> <br>
Vendor Details: <textarea> </textarea> <br> <br>
<button ng-click="myFunc()">Click to enter Observations</button>
</form>

<div ng-show="showMe">
    <br> <br> <form>
    Detailed Observation <input type="text">
    <button type="submit" ng-click="myFunc()"> Next Observation </button>
    <button type="submit"> Finish </button>
    </form>
</div>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.showMe = false;
    $scope.myFunc = function() {
        $scope.showMe = true;
    }
});
</script>
</body>
</html>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135

1 Answers1

0

If I understand you correctly, you don't need to change the UI (e.g. render the form again or something) but reset the form after every "next" click.

If so, my suggestion is to hold an array of observations. Clicking on "next" will push the input's value into this array and clear the input. Clicking on finish will do something (log currently) with the array.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

<form>
Vendor Name: <input type="text"> <br> <br>
Vendor Details: <textarea> </textarea> <br> <br>
<button ng-click="myFunc()">Click to enter Observations</button>
</form>

<div ng-show="showMe">
    <br> <br> <form>
    Detailed Observation <input type="text" ng-model="observation">
    <button type="submit" ng-click="nextObservation()"> Next Observation </button>
    <button type="submit" ng-click="finish()"> Finish </button>
    </form>
</div>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.observations = [];
    $scope.showMe = false;
    $scope.myFunc = function() {
        $scope.showMe = true;
    }
    $scope.nextObservation = function() {
      if ($scope.observation) {
          $scope.observations.push($scope.observation);
          $scope.observation = '';
      }
    }
    
    $scope.finish = function() {
        $scope.nextObservation();
        console.log($scope.observations);
    }
});
</script>
</body>
</html>

Update

In order to keep the previous values but add a new one on click on "next", you can use that array with ng-repeat to render the ui based on that array. Notice that you need track by $index this time to avoid entities duplications (For more info: https://stackoverflow.com/a/26518721/863110)

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
  <div ng-app="myApp" ng-controller="myCtrl">
    <form>
      Vendor Name: <input type="text"> <br> <br> Vendor Details: <textarea> </textarea> <br> <br>
      <button ng-click="myFunc()">Click to enter Observations</button>
    </form>

    <div ng-repeat="observation in observations track by $index">
      <br> <br>
      <form>
        Detailed Observation <input type="text" ng-model="observations[$index]">
        <button type="submit" ng-click="nextObservation($index)" ng-if="$last"> Next Observation </button>
        <button type="submit" ng-click="finish()" ng-if="$last"> Finish </button>
      </form>
    </div>
  </div>
  <script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
      $scope.observations = [];

      $scope.myFunc = function() {
        $scope.observations.push('');
      }
      $scope.nextObservation = function($index) {
        $scope.observations[$index]
        if ($scope.observations[$index]) {
          $scope.observations.push('');
        }
      }
      $scope.finish = function() {
        console.log($scope.observations);
      }
    });
  </script>
</body>
</html>
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135