【问题标题】:angular js array in model模型中的角度js数组
【发布时间】:2015-03-11 10:04:04
【问题描述】:

我正在使用 ng-repeat 重复一个表单。重复是创造一个新的形式。 我想通过单击提交所有表单数据。我无法找到如何在数组中分配多个值。

这是我的表格:

<div id="check" ng-repeat="screen in screens">
                   <input type="hidden" name="scrnid" ng-model="screen.scrnid" value="{{ screen.scrnid }}" />
            <label>Name:</label>
            <input type="text" ng-model="screen.bdayname" placeholder="Name" ng-required/>
            <label>Date:</label>
            <input type="date" ng-model="screen.bdaydate" placeholder="Date" ng-required/>
            <br/>
               </div>

               <button class="btn" type="submit" ng-click="newBirthday()">Save</button>
</div>
<div class="container">
        <input type="button" class="btn" ng-click="createScreen()" /><i class="icon-plus"></i>Add
</div>

它是我的控制器

app.controller('main', function($scope){ 

    $scope.screens = [{scrnid: "1"}];
    $scope.scrnid = 1;
    $scope.createScreen = function()
    {
            $scope.screens.push({scrnid: "" + ($scope.screens.length + 1)});            
    };

    $scope.bdays = [];        
    // Create the function to push the data into the "bdays" array

    $scope.newBirthday = function(){        

        $scope.bdays.push($scope.screens.length);  // how to push mutiple records in array dynamically

        $scope.screen.bdayname = '';
        $scope.screen.bdaydate = '';
    };
});

我们将不胜感激...

【问题讨论】:

  • assign multiple values in array,这多个值是什么?
  • $scope.bdays.push() 在这个数组中如果我想发送多个值,我会这样做: $scope.bdays.push([ {'name': 'name1' }, {'name': 'name2'} ]);这是 angularjs 的做法。但是我应该怎么做才能将重复的html表单中的值插入到这个数组中......

标签: javascript jquery angularjs angularjs-ng-model


【解决方案1】:

我已编辑代码以将对象存储到 bday 范围变量中

var app=angular.module("birthdayToDo",[]);


app.controller('main', function($scope){ 

   $scope.screens = [{scrnid: "1"}];
  $scope.scrnid = 1;
    $scope.createScreen = function(){
            $scope.screens.push({scrnid: "" + ($scope.screens.length + 1)});  

            console.log($scope.screens);          
        };

        $scope.bdays = [];        


    $scope.newBirthday = function(){

        for(i=0;i<$scope.screens.length;i++){
            $scope.bdays.push($scope.screens[i])
        }
        console.log($scope.bdays);


    };


    $scope.remove=function(item){ 
      var index=$scope.bdays.indexOf(item)
      $scope.bdays.splice(index,1);     
    }



});

【讨论】:

  • 这里你已经完成了单一表格。要传递单一形式的值,我可以轻松地传递它们。但是,如果您注意到我正在重复此表单,现在我想提交所有表单数据。我将如何通过表单重复来管理它...
  • 检查这个小提琴jsfiddle.net/9fqfh8ty这里使用了多个表单,每个表单都有一个保存操作,我还编辑了上面的答案,如果这能解决你的问题,请告诉我
  • 这里有三个表单,其中三个按钮设置目标按钮值以填充数组。我想要三个带有单个按钮的表单,并在数组中添加所有三个记录值。你有什么想法吗...
  • 您的意思是您有 3 个表单,但只有一个保存按钮?
  • 是的,这三个表单是在 ng-repeat 中创建的,这意味着我有一个正在重复的表单 n 有一个保存按钮我想通过单击将这些表单保存到数据库,并重复表单是动态的。表格可以是 3 或 4 或 5 ....
猜你喜欢
  • 2013-01-09
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多