subhra subhra - 2 months ago 6
AngularJS Question

can not add new element using ng-repeat in Angular.js

I need one help. I need to create extra input element using

ng-repeat
in angular.js. I did something but its not working. I explaining my code below.

<div>
<ul>
<li ng-repeat="r in arr">
<input type="text" ng-model="r.uname">
</li>
</ul>
</div>
<button type="button" id="btn" ng-click="create();">Add</button>


Here I need when user will click on add button the new input field will create just below the first one. My scripting side code is given below.

<script type="text/javascript">
function ContactController($scope){
$scope.arr=[{uname:null}];
$scope.create=function(){
console.log('hii');
$scope.arr.push({
uname:null
});
console.log('hi',$scope.arr);
}
}
</script>


But here i am unable to generate the new input type element. Please help me.

Answer

I don't see an error in your code. it works: http://jsfiddle.net/Lvc0u55v/9536/

$scope.arr=[{uname: null}];
  $scope.create = function(){
    console.log('hii');
    $scope.arr.push({
      uname: null
    });
    console.log('hi',$scope.arr);
  }