Angularjs - ng-repeat 和嵌套范围

Angularjs - ng-repeat and nested scope

我是 angularjs 的新手,正在尝试掌握在嵌套范围内访问数据的概念。 这是包含对象嵌套范围 "contacts":

的主范围 "company"
$scope.company = 
      {
        companyName: "",
        contacts: {
          name: "",
          email: ""
        }
      }

我正在尝试在每一行中使用来自 "contacts" 范围的数据生成一个 table。

 <tr ng-repeat="contact in company.contacts">
                    <td>
                        <input type="text" class="form-control" ng-model="contact.name"/>
                    </td>
                    <td>
                        <input type="text" class="form-control" ng-model="contact.email"/>
                    </td>
                </tr>

但似乎我在这里做错了什么,因为当我尝试显示来自主范围 {{company}} 的数据时,实际上什么也没有发生。

我真的需要一些建议。谢谢。

您不想为此使用循环。 只需使用 company.contacts.namecompany.contacts.email

但是我怀疑您可能想要多个联系人,因此是复数联系人。为此,您需要将联系人设为一个数组。 对于数组,您可以使用重复。

像这样:

$scope.company = 
      {
        companyName: "",
        contacts: [
         {
          name: "",
          email: ""
         },
         {
          name: "",
          email: ""
         }
        ]
      }

题外话:如果您只是在学习 Angular1 并且还没有正在处理的大型项目,请切换到 Angular2+(当前为 6)。 AngularJs (1.x) 是一个仅提供一年半长期支持的框架。 (AngularJs (1.x) 和 Angular (>= 2 ) 是不同的框架。)