2016-03-15 62 views
1

刚开始学习角度,我现在很难过。Angular:访问数据

我不明白角度如何访问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> 

但像本教程不,我不从控制器加载数据。在我的情况下,它已经在那里。我只是需要去操纵它。怎么样?

回答

4

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

1

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