Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从同一模型AngularJS添加多行_Angularjs_Node.js - Fatal编程技术网

从同一模型AngularJS添加多行

从同一模型AngularJS添加多行,angularjs,node.js,Angularjs,Node.js,考虑到我有一个用户列表要添加到我的系统中,我不想一个接一个地进行。然后我想看到一个用户表单(姓名、电子邮件、密码字段),在行的末尾我看到“添加另一行”按钮 通过单击“添加另一行”按钮,将在现有用户表单之后再添加一个用户表单 在AngularJS中,如何仅通过一个POST请求发送多个用户?这是一些非常基本的AngularJS内容。以下是一个例子: <form name="usersForm" ng-submit="save()"> <div ng-repeat="user

考虑到我有一个用户列表要添加到我的系统中,我不想一个接一个地进行。然后我想看到一个用户表单(姓名、电子邮件、密码字段),在行的末尾我看到“添加另一行”按钮

通过单击“添加另一行”按钮,将在现有用户表单之后再添加一个用户表单


在AngularJS中,如何仅通过一个POST请求发送多个用户?

这是一些非常基本的AngularJS内容。以下是一个例子:

<form name="usersForm" ng-submit="save()">
    <div ng-repeat="user in users">
        <label>Name:</label>
        <input type="text" ng-model="user.name" required />
        <br/>
        <label>Email:</label>
        <input type="email" ng-model="user.email" required />
        <hr/>
    </div>
    <button ng-click="newUser($event)">Add another one</button>

    <input type="submit" value="Save" ng-disabled="usersForm.$invalid || !users.length" />
</form>

您的控制器中应该有一个用户阵列,每次按“添加另一个用户”时,您都可以向阵列中添加一个新用户

控制器中的代码:

    $scope.users = [{ name: "1" , email: "email1", password:"pas1" }, { name: "2" , email: "email2", password:"pas2"}];;

$scope.addUser = function () {

    $scope.users.push({ name: "" , email: "", password:""  });
};
现在,在html中会有一个列表,其中包含数组中绑定的用户。每次添加一个新用户都会在html中添加一行新行,其中的视图绑定到所添加的新用户的参数上

 <form ng-submit="submit()">
    <div class="container">
        <div class="col-md-5">
            <li class="list-group-item col-xs-12" data-ng-repeat="user in users">
                <input type="text" class="form-control" placeholder="Username" ng-model="user.name">
                <input type="text" class="form-control" placeholder="Password" ng-model="user.password">
                <input type="text" class="form-control" placeholder="Email" ng-model="user.email">
            </li>
            <button class="col-xs-12" ng-click="addUser()">Add Another User</button>

    </div>


</form>

  • 添加其他用户

    将函数submit也添加到您的控制器,以便按照您希望的用户阵列方式进行提交。希望有帮助。

    您可以使用
    $http.post()
    发布任何您想要的内容。有些东西不起作用了吗? 如果您的用户存储在$scope.users中,则在“保存”方法中,您只需编写:

    $http.post('/someUrl', $scope.users).
      success(function(data, status, headers, config) {
        console.log('POSTed!');
      }).
      error(function(data, status, headers, config) {
        console.log('Failed!');
      });
    

    您的JSON对象将被发送到服务器。

    您可以在POST请求中提交几乎所有内容,包括用户列表。有没有一个特定的部分不适合你?克里斯蒂娜,我还没有任何代码。在过去,当我使用php时,我们必须将输入名称设置为“users[]”这样才能在请求中获取数组。现在,角度控制器将处理它,总结。酷!非常感谢。
    $http.post('/someUrl', $scope.users).
      success(function(data, status, headers, config) {
        console.log('POSTed!');
      }).
      error(function(data, status, headers, config) {
        console.log('Failed!');
      });