0

Actually am the starter in angularjs.

I have created one form and it contains two input boxes.

Below the input boxes,

<input ng-model="id.todoList" type="text" name="input" placeholder="Enter tasks here">
<input ng-model="id.todoList1" type="text" name="input" placeholder="Enter tasks here">

After proceed the input values,

I want the values to be stored as an array. I did!!!!

I can able to push the form inputs whenever i click the submit button.

But, the main problem is...

Suppose i have added the 3 user details, If i go to add the other details means, the stored 3 user details also changes.

Can anyone help on this?

Attached the fiddler link :Fiddler Link

3 Answers 3

1

Try to create a array of json objects while pushing the todoList & todoList1

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.todos = []
  $scope.id = {   // Creating id object
    todoList: "",
    todoList1: ""
  }

  $scope.addTodo = function() {
    // Creating a json array of objects with 
    // key todoList & todoList1
    $scope.todos.push({
      todoList: $scope.id.todoList,
      todoList1: $scope.id.todoList1,
    })
  }

  $scope.clearAll = function() {
    $scope.todos = []
  }

});

DEMO

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

Comments

1

Try this :

  <body ng-app="plunker">
    <div class="container" ng-controller="MainCtrl">
      <div class="main">
        <p>Todo App</p>
        <input ng-model="id.todoList" type="text" name="input" placeholder="Enter tasks here">
        <input ng-model="id.todoList1" type="text" name="input" placeholder="Enter tasks here">
        <button ng-click="addTodo()">Add</button>
        <ul>
          <li ng-repeat="todo in todos track by $index">
            {{todo}}
          </li>
        </ul>
      </div>
    </div>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script>
  
  var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.todos = []
  $scope.todoList = "";

  $scope.addTodo = function(){
    var temp =  $scope.id;
    $scope.todos.push({"todoList":$scope.id.todoList,"todoList1":$scope.id.todoList1});
    console.log($scope.todos);
    $scope.id.todoList = '';
    $scope.id.todoList1 = '';
  }

  


});
</script>
  </body>

Comments

0

you can change to this

<button ng-click="addTodo(id)">Add</button>

and get value of form in js

id.todoList
id.todoLIst1

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.