2

Upon pressing add button in myform, I want to get the value in the input Name using ng-model. I am using append in my form to add the next column. However, I am facing issues in achieving this through the code below

function add(){
var stre;
stre="Name: <input type='text' ng-model='Name'><br>"
+"Result Name: <input type='text' value={{Name}}>"
$("#test").append(stre);
}
<!DOCTYPE html>
<html ng-app>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<body>

<div id="test">
<button type="button" onclick="add()">add</button><br>
</div>

</body>
</html>

2
  • How about ng-repeat ? Commented Feb 24, 2016 at 8:22
  • Then how to get value in input? can u make example? Commented Feb 25, 2016 at 9:06

1 Answer 1

2

Use ng-repeat instead of append

The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item.

var myApp = angular.module('myApp', []);
myApp.controller('myController', myController);

function myController($scope) {
  $scope.elements = [];
  $scope.add = function() {
    $scope.elements.push({});
  };

}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller='myController'>
    <div id="test">
      <div ng-repeat='elem in elements'>
        Name:
        <input type='text' ng-model='elem.name'>
        <br>Result Name:
        <input type='text' value={{elem.name}}>
        <hr>
      </div>
      <button type="button" ng-click="add()">add</button>
    </div>
  </div>
</div>

Fiddle here

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.