简体   繁体   English

AngularJS数据推送

[英]AngularJS data push

I have the following AngularJS app. 我有以下AngularJS应用。

From $scope.forms I create some inputs on the html part using ng-repeat . $scope.forms我使用ng-repeat在html部分上创建了一些输入。

The thing I dont understand is how to push the data from attributes: ["title", "firstname", "lastname", "address"] inputs into values: [] 我不明白的是如何从attributes: ["title", "firstname", "lastname", "address"]推送数据attributes: ["title", "firstname", "lastname", "address"]输入到values: []

Can someone explain me how to push the input values into my values key? 有人可以解释一下如何将输入值推入我的值键吗? Or maybe if there is a best solution to explain? 或者,如果有最佳解决方案可以解释?

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
    }   ]; }

http://jsfiddle.net/U3pVM/18198/ http://jsfiddle.net/U3pVM/18198/

See @Grundy's answer for a direct approach where you don't have to change your model. 请参见@Grundy的答案 ,以了解无需更改模型的直接方法。

Do allow me to suggest a different approach though (that also includes the fact that you need to use ng-model to bind the input 's value ): model the attribute + value combination as an actual thing. 不过,请允许我提出一种不同的方法(这还包括您需要使用ng-model绑定inputvalue的事实):将属性+值组合建模为实际的事物。 So eg: 所以例如:

$scope.forms = [
    {
        title: "Something",
        pairs: [{label: "title", value: ""}, {label: "firstname", value: ""}]
    }
];

This view model is much easier to bind to in a view: 此视图模型更容易绑定到视图中:

<div ng-repeat="pair in form.pairs">
    <input type="text" placeholder="{{pair.label}}" ng-model="pair.value" />
</div>

The reason I suggest this is because the $scope works best IMO if it's tailored to being bound to in views. 我建议这样做的原因是,如果$scope是专门为绑定到视图而设计的,则它最适合IMO。 If you need the other format (ie a values array) perhaps to send it off to a back end service, you'd best map the view model back to the appropriate data format. 如果您可能需要其他格式(例如, values数组)以将其发送给后端服务,则最好将视图模型映射回适当的数据格式。 For example: 例如:

var values = $scope.forms[0].pairs.map(function(p) {
    return p.value;
});

See this forked fiddle for a full example. 有关完整示例,请参见此分叉的小提琴

You should see about ng-model . 您应该看到有关ng-model的信息 as model you can use values[$index] so, in values array values in same order as in attributes . 作为模型,您可以使用values[$index]因此,在values数组值中的使用顺序与attributes顺序相同。

 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 } ]; } 
 body { font-family: courier new; } h1 { font-size: 24px; margin: 0 0 20px; } h2 { font-size: 18px; } .form { margin: 0 0 25px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <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> {{form.attributes}} {{form.values}} </div> </div> </div> 

I have added the changes to your fiddle: 我已将更改添加到您的小提琴中:

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

http://jsfiddle.net/U3pVM/18201/ http://jsfiddle.net/U3pVM/18201/

It's simple : Here is updated fiddle 很简单:这里是更新的小提琴

Just added an ng-model that points at the exact same index in attributes array and that of in values array. 刚刚添加了一个ng-model ,该ng-model指向attributes数组和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">
              <input type="text" ng-model="form.values[$index]"  placeholder="{{input}}" />
          </div>
        </div>
        <button ng-click="haha()">Test!</button>
    </div>
  </div>

And no change in JS. JS也没有变化。 Only added new function haha() to test it out 只添加了新函数haha()进行测试

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM