2013-12-13 133 views
1

我有這樣一個觀點:角NG-重複的輸入

<table class="table"> 
    <tr data-ng-repeat="exercise in exercises x"> 
     <td> 
      <input type="number" data-ng-model="?????????" /> 
     </td> 
     <td> 
      {{exercise.Name}} 
     </td> 
    </tr> 
    </table> 

我不知道我應該把儘可能data-ng-model這樣我就可以使用雙向即輸入的值,在數據綁定我控制器?

我試過data-ng-model="{{exercise.Name}}"但是導致了錯誤。

另外,如何在控制器中引用某些輸入?我可以這樣做:$scope.InputOne = ...

+0

我遠離PC) – hyperN

回答

2

用於雙向綁定使用ng模型,單向綁定使用ng-bind或{{}}括號。

這個example演示瞭如何使用這兩種方式以及如何獲取對象的信息。

ps:控制器不應該直接「看見」視圖。

<body data-ng-app="app"> 
<div data-ng-controller="TestCtrl"> 
    <table> 
    <tbody> 
     <tr data-ng-repeat="exercise in exercises"> 
     <td> 
      <input type="number" data-ng-model="exercise.Name" /> 
     </td> 
     <td data-ng-bind="exercise.Name"></td> 
     <td><button data-ng-click="getInformation($index)">Get information</button></td> 
     </tr> 
    </tbody> 
    </table> 
</div> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script> 

var app = angular.module('app', []); 

app.controller('TestCtrl', function($scope) { 

$scope.exercises = [{Name:1}, 
        {Name:2}, 
        {Name:3}, 
        {Name:4} 
]; 

$scope.getInformation = function(index) { 
    alert($scope.exercises[index].Name); 
}   
}); 
+0

的[步驟10](http://docs.angularjs.org/tutorial/step_10)非常感謝您的詳細解答! – hyperN

5

使用data-ng-model="exercise.Name"而沒有{{}}括號。

我建議你從angular tutorial開始。

+0

謝謝,請你能告訴我怎樣才能在控制器特定輸入數據? – hyperN

+0

請參閱上述教程 – PixnBits