我是Angular的新手,
然后我想用javascript获取模型数据。
我想是否可以使用$ scope和html元素使用javascript获取模型值?
这是方法吗?
<div class="tax-concept-form" ng-controller="TaxesController as taxes" ng-init="taxes.init()">
<table cellspacing="0" class="taxes-table table ctrl-3" id="tabla_edicion" tabindex="">
<tbody>
<tr ng-repeat="_tax in taxes.list" class="javorai-master-detail-row item-selectable" ng-model="_tax" id="row_{{_tax.id}}">
<td>{{_tax.tax.description}}</td>
<td>{{_tax.tax.rate}}</td>
</tr>
</tbody>
</table>
<script>
//I have a angular $scope
//some javascript
var el = document.getElementById('row_1');
//I want the tax of row_1
</script>
答案 0 :(得分:1)
不建议在控制器中使用DOM元素:
> Do not use controllers to:
Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.
Format input — Use angular form controls instead.
Filter output — Use angular filters instead.
Share code or state across controllers — Use angular services instead.
Manage the life-cycle of other components (for example, to create service instances).
而是使用数据绑定来分离视图和业务逻辑。有关详细信息,请参阅developer guide。