AngularJS:访问内部数组

时间:2017-12-20 10:49:28

标签: arrays angularjs

这是一个简单的HTML代码,它工作正常。现在我有一个AngularJS数组,我可以访问和绑定像$scope.ID$scope.text这样的变量。

但是如何将值绑定到$scope.articles数组。具有相关价值。

例如
如果我改变f0的值,(只是示例它不起作用)$scope.articles.[0].betrag_gnetto值应该改变
如果我改变f1的值,(只是示例它不起作用)$scope.articles.[1].betrag_netto值应该改变

HTML代码(我已经通过ng-repear循环创建了这些字段)

<input type="text" ng-model="betrag_netto0" id="f0"> 
<input type="text" ng-model="betrag_netto1" id="f1">
<span ng-bind="betrag_netto0"></span>
<span ng-bind="betrag_netto1"></span>

AngularJS数组看起来像

{  
   "isInternalInvoice":1,
      "name":"Rechnung",
      "ID":"5cd45e86",
      "text":null,
      "countArticles":2,
      "articles":[  
         {  
            "ID":"130.123",
            "betrag_netto":"123987"
         },
         {  
            "ID":"131.123",
            "betrag_netto":"1"
         }
      ]
   }

2 个答案:

答案 0 :(得分:0)

尝试package models; import java.util.HashSet; import java.util.Set; public class Book { public Integer id; public String title; public Integer price; public String author; public Book() { } public Book(Integer id, String title, Integer price, String author) { this.id = id; this.title = title; this.price = price; this.author = author; } private static Set<Book> books; static { books= new HashSet<>(); books.add(new Book(1,"C++",1100,"Abc")); books.add(new Book(2,"JAVA",1100,"Scd")); } public static Set<Book> allBook(){ return books; } public static Book findById(Integer id){ for(Book books:books) { if (id.equals(books.id)){ return books; } } return null; } public static void add(Book book){ books.add(book); } public static boolean remove(Book book){ return books.remove(book); }

它有效

答案 1 :(得分:0)

使用ng-repeat创建与articles数组中存在的对象相对应的输入字段,这将使其成为双向绑定。

&#13;
&#13;
var app = angular.module('plunker', []);

app.controller('ApplicationController', function($scope) {
    $scope.obj = {
        "isInternalInvoice": 1,
        "name": "Rechnung",
        "ID": "5cd45e86",
        "text": null,
        "countArticles": 2,
        "articles": [{
            "ID": "130.123",
            "betrag_netto": "123987"
        }, {
            "ID": "131.123",
            "betrag_netto": "1"
        }]
    };
});
&#13;
<!doctype html>
<html ng-app="plunker">

<head>
    <meta charset="utf-8">
    <title>AngularJS Plunker</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
</head>

<body>
    <div class="container" ng-controller="ApplicationController">
        <div class="row" ng-repeat="article in obj.articles">
            <input type="text" ng-model="article.betrag_netto" id="f{{$index}}"> <span ng-bind="article.betrag_netto"></span> </div>
    </div>
</body>

</html>
&#13;
&#13;
&#13;