Javascript AngularJS数据推送

Javascript AngularJS数据推送,javascript,angularjs,Javascript,Angularjs,我有以下AngularJS应用程序 从$scope.forms中,我使用ng repeat在html部件上创建一些输入 我不明白的是如何将数据从属性:[“title”、“firstname”、“lastname”、“address”]输入推送到值:[] 有人能解释一下如何将输入值推入我的值键吗? 或者,如果有一个最好的解决方案来解释 function sampleCtrl($scope) { $scope.forms = [ { title: "Something",

我有以下AngularJS应用程序

$scope.forms
中,我使用
ng repeat
在html部件上创建一些输入

我不明白的是如何将数据从
属性:[“title”、“firstname”、“lastname”、“address”]
输入推送到
值:[]

有人能解释一下如何将输入值推入我的值键吗? 或者,如果有一个最好的解决方案来解释

function sampleCtrl($scope) {

  $scope.forms = [
    {
      title: "Something",
      attributes: ["title", "firstname", "lastname", "address"],
      values: [] //here i want the values from the attributes
    },
    {
      title: "Something else",
      attributes: ["title", "name", "job", "address"],
      values: [] //here i want the values from the attributes
    }   ]; }

有关无需更改模型的直接方法,请参阅

不过,请允许我建议一种不同的方法(这还包括您需要用于绑定
输入
):将属性+值组合建模为实际情况。例如:

$scope.forms = [
    {
        title: "Something",
        pairs: [{label: "title", value: ""}, {label: "firstname", value: ""}]
    }
];
此视图模型在视图中更易于绑定:

<div ng-repeat="pair in form.pairs">
    <input type="text" placeholder="{{pair.label}}" ng-model="pair.value" />
</div>
有关完整示例,请参阅。

您应该了解。作为模型,您可以使用
值[$index]
因此,在
值中
数组值的顺序与
属性中的顺序相同

函数sampleCtrl($scope){
$scope.forms=[
{
标题:“某物”,
属性:[“标题”、“名字”、“姓氏”、“地址”],
values:[]//这里我想要属性中的值
},
{
标题:“其他东西”,
属性:[“职务”、“姓名”、“职务”、“地址”],
values:[]//这里我想要属性中的值
}
];
}
正文{
字体系列:信使新;
}
h1{
字体大小:24px;
利润率:0.20px;
}
氢{
字号:18px;
}
.表格{
边际:0.25px;
}

形式
{{form.title}
{{form.attributes}}
{{form.values}}

我已将更改添加到您的小提琴上:

<div ng-app>
    <h1>Form</h1>
    <div ng-controller="sampleCtrl">

        <div class="form" ng-repeat="form in forms">
          <h2>{{form.title}}</h2>     
          <div ng-repeat="input in form.attributes" ng-init="mod = []">
              <input type="text" placeholder="{{input}}" ng-init="mod[$index] = form.values[$index]" ng-model="mod[$index]"/>
          </div>
       </div>     
   </div>
</div>

形式
{{form.title}
很简单:这里是

刚刚添加了一个
ng模型
,该模型指向
属性
数组中与
数组中完全相同的索引

  <div ng-app>
    <h1>Form</h1>
    <div ng-controller="sampleCtrl">

        <div class="form" ng-repeat="form in forms">
          <h2>{{form.title}}</h2>     
          <div ng-repeat="input in form.attributes">
              <input type="text" ng-model="form.values[$index]"  placeholder="{{input}}" />
          </div>
        </div>
        <button ng-click="haha()">Test!</button>
    </div>
  </div>

是的,+1,与我的解决方案类似,只有一个解决方案是OP不必使用两个单独的数组更改当前的
$scope
格式。@Jeroen,是的,在某些情况下,我们无法更改结构:-)任何方式主要问题OP-他不使用
ng model
:-)为什么使用这么多ng init?在这种情况下,当您更改输入中的值时,它们不会应用于
arrayyah。刚刚看到您的答案,可以使用form.values数组来完成。我以为我们需要另一个数组来存储值,但我太天真了3无论如何,在每个嵌套的ng repepeat迭代中创建新的
mod
数组:-)只要看小提琴:哦,是的。div被渲染多次并每次初始化。应该在ng重复上完成ng init。无论如何,谢谢你。:)除了
哈哈
,你和我的答案有什么不同吗?:-)
  <div ng-app>
    <h1>Form</h1>
    <div ng-controller="sampleCtrl">

        <div class="form" ng-repeat="form in forms">
          <h2>{{form.title}}</h2>     
          <div ng-repeat="input in form.attributes">
              <input type="text" ng-model="form.values[$index]"  placeholder="{{input}}" />
          </div>
        </div>
        <button ng-click="haha()">Test!</button>
    </div>
  </div>
function sampleCtrl($scope) {

  $scope.forms = [
    {
      title: "Something",
      attributes: ["title", "firstname", "lastname", "address"],
      values: [] //here i want the values from the attributes
    },
    {
      title: "Something else",
      attributes: ["title", "name", "job", "address"],
      values: [] //here i want the values from the attributes
    }
  ];

    $scope.haha = function(){
        console.log($scope.forms);
    }

}