当点击按钮时,如何在angularjs中获得json文件

How to get as json file in angularjs when clicking button?

本文关键字:angularjs 文件 json 按钮      更新时间:2023-09-26

实际上我是angularjs中的starter .

我创建了一个表单,它包含两个输入框。

输入框下方,

<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">

继续输入值后,

我希望将值存储为数组。我了! !

我可以在点击提交按钮时推送表单输入。

但是,主要的问题是……

假设我已经添加了3个用户详细信息,如果我去添加其他详细信息的方式,存储的3个用户详细信息也会改变。

有人能帮忙吗?

附加了提琴手链接:提琴手链接

尝试创建一个json对象数组,同时推入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 = []
  }
});

试试这个:

  <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>

你可以改成这个

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

和get form的值

id.todoList
id.todoLIst1