2015-01-20 33 views
1

我是新角度的,AngularJS - 通過html元素獲取ng模型

然後我想使用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> 
+2

ng模型在'tr'上是無用的。 ng-model用於表單元素(輸入,textarea,select),以便用用戶輸入/選擇的內容填充模型。用戶不會在'tr'中輸入任何內容。你爲什麼要用dom來查找模型中數組的第n個元素?只需在控制器中使用'$ scope.taxes.list [1]'。 – 2015-01-20 21:54:10

+0

很好..我怎麼能做一個TR的綁定數據?抱歉如果我的問題很糟糕。 – jrey 2015-01-20 21:56:39

+0

你已經在做。只需刪除無用的ng模型:每個tr有兩個tds,每個td顯示當前稅收對象的n屬性。有什麼問題?你想達到什麼目的? – 2015-01-20 21:59:15

回答

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