Angular Hide/Show Input Test

by jacobwsmith

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div ng-controller="myCtrl">
  <form name="form">
  
    <div ng-repeat="item in data" ng-show="$index === showIndex">
      <ng-form name="nameForm">
        Name {{$index+1}}:
        <input name="name" ng-model="item.name" required>
        <pre>{{nameForm.name | json}}</pre>
        <pre>$error: {{nameForm.name.$error}}, $dirty: {{nameForm.name.$dirty}}, $pristine: {{nameForm.name.$pristine}}</pre>
      </ng-form>
    </div>
  
  </form>
  <hr>
  <p>
    <span ng-repeat="item in data">
      <a href="#" ng-click="setIndex($index)">Tab {{$index +1}}</a> |
    </span>
  </p>
  <p>
    <a href="#" ng-click="addName()">+ Add</a>
  </p>
</div>

CSS

html,
body {
  margin: 10px;
}

input {
  margin: 5px;
}

JavaScript

var app = angular.module('app', []).controller('myCtrl', function ($scope) {
  $scope.data = [{name: ''}, {name: ''}];
  $scope.show = true;
  $scope.showIndex = 0;
  $scope.setIndex = function(index){
    $scope.showIndex = index;
  }
  $scope.addName= function(){
    // ugh - resets the $dirty value
    //var temp = angular.copy($scope.data);
    //temp.push({name: ''})
    //$scope.data = temp;
    $scope.data.push({name: ''})
  }
  
});