0

I have 2 input text

<input type="text" ng-model="name">
<input type="text" ng-model="lastname">

I have a table :

             <table>
                <thead>
                    <th>Name</th>
                    <th>Last Name</th>
                </thead>
                <tbody>
                    <tr ng-repeat="row in customers">
                        <td>{{row.fname}}</td>
                        <td>{{row.lname}}</td>
                    </tr>
                </tbody>
            </table>                   

I want to push data from name and last name text boxes into a row in the table using angularjs, any help?

2
  • do you have some button which pushes the data into the $scope.customers ? Commented May 17, 2017 at 7:04
  • yes, a button calling a function and stuck here Commented May 17, 2017 at 7:05

2 Answers 2

2

Just create a new customer object on the click of a button and push it into the customers array. When you add the customer object, the row shall be created.

<button ng-click="createCustomer()">Push</button>

In the controller,

$scope.createCustomer = function(){
 customers.push(new Customer($scope.name,$scope.lastname));
}

var Customer = function(name,lastname){
 this.fname = name;
 this.lname = lastname;
}
Sign up to request clarification or add additional context in comments.

3 Comments

example please ?
edited the answer for your reference and added the code. By creating a new object of customer and adding it into the array, you tell the ng-repeat in view to add a new row
the 2 solutions working, but the second one is more close to my code (habits), working thank you !
1

In your controller create a function which will store customer in customers array.

in template

<input type="text" ng-model="name">
<input type="text" ng-model="lastname">
<button ng-click="addCustomer(name, lastname)>Add</button>

in controller

$scope.customers = [];
$scope.addCustomer(name, lastname){
     $scope.customers.push({ fname:name, lname:lastname });
}

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.