角度:访问数据

Angular: access data

本文关键字:数据 访问 角度      更新时间:2023-09-26

刚开始学习角度,我现在很困惑。

我不明白角度如何访问 DOM 数据。

在所有示例中,我都看到数据在控制器中初始化:

phonecatApp.controller('PhoneListCtrl', function ($scope) {
  $scope.phones = [
    {'name': 'Nexus S',
     'snippet': 'Fast just got faster with Nexus S.'},
    {'name': 'Motorola XOOM™ with Wi-Fi',
     'snippet': 'The Next, Next Generation tablet.'},
    {'name': 'MOTOROLA XOOM™',
     'snippet': 'The Next, Next Generation tablet.'}
  ];
});

例如,如果我有一个从其他来源填充的表格怎么办。

<table>
    <tr>
        <td> John </td>
        <td> johnson </td>
        <td> 30 </td>
    </tr>
    <tr>
        <td> David </td>
        <td> Davidson </td>
        <td> 23 </td>
    </tr>
    ...
</table>

假设我想在用户单击某个按钮时对这些表行执行某些操作(例如,筛选其中一些)。如何获取控制器中表中的所有数据。

我可以像教程中一样在中继器上应用过滤器

<li ng-repeat="phone in phones | filter:query">
   {{phone.name}}
   <p>{{phone.snippet}}</p>
</li>

但我不像教程那样从控制器加载数据。就我而言,它已经在那里了。我只需要得到它进行操作。如何?

简单的答案是,你不能。Angular 只能从控制器内部(或使用 ng-init,但控制器使其更容易)操作绑定到$scope的数据。

Angular 不访问 DOM 来检索数据,它访问你传递给它的数据(通常在控制器内部)并使用它来生成 DOM。