2015-11-16 40 views
0

我需要在點擊一個表中的行獲得的價值和彈出式窗口中顯示它獲取價值並把它傳遞給彈出

HTML:

<md-data-table-container> 
    <table md-data-table class="md-primary" md-progress="deferred"> 
     <thead md-order="query.order" md-trigger="onorderchange"> 
      <tr> 
       <th name="Task To Be Done"></th> 
       <th name="Office"></th> 
       <th name="Due Date"></th> 
      </tr> 
     </thead> 
     <tbody ng-click="showAlert($event)"> 
      <tr ng-repeat="dessert in desserts.data" ng-click="showAlert($index)" flex-sm="100" flex-md="100" flex-gt-md="auto"> 
       <td>{{dessert.task}}</td> 
       <td></td> 
       <td>{{dessert.due_on}}</td> 
      </tr> 
     </tbody> 
    </table> 
</md-data-table-container> 

JS:

$scope.desserts = { 
    "count": 6, 
    "data": [{ 
     "task": "Frozen yogurt", 
     "type": "Ice cream" 
    }, { 
     "task": "Ice cream sandwich", 
     "type": "Ice cream" 
    }, { 
     "task": "Eclair", 
     "type": "Pastry" 
    }, { 
     "task": "Cupcake", 
     "type": "Pastry" 
    }, { 
     "task": "Jelly bean", 
     "type": "Candy" 
    }, { 
     "task": "Lollipop", 
     "type": "Candy" 
    }, { 
     "task": "Honeycomb", 
     "type": "Other" 
    }] 
}; 
$scope.showAlert = function (index) { 
    $scope.obj = $scope.desserts.data[2]; 
    $scope.task = $scope.obj.task; 
    alert($scope.task); 
    console.log($scope.task); 
}; 

問題在我的代碼是,我可以得到我指定「。數據[2]」的數組上的值。實際上,當我點擊表格中的一行時,我需要顯示該值以彈出「sweetAlert」。是否有任何解決方案

+0

這不是很清楚。你有一個沙漠陣列,但沒有顯示它,你有一個點擊調用$索引,但沒有重複圍繞它。甜蜜的警報來自哪裏?你從不打電話或使用它?你怎麼認爲有人可能會告訴你什麼是錯的。用你的代碼創建一個plunkr或jsfiddle。 – Shaun

+0

檢查我的更新後的代碼 –

+0

添加了答案。我不知道「sweetAlert」是什麼意思。 – Shaun

回答

3

不要通過$index,因爲如果底層數據發生變化,這可能會有點危險,在這種情況下,它只是迫使你重新從數組中獲取物品。

將想要顯示的實際項目傳遞迴警報。

<tr ng-repeat="dessert in desserts.data" ng-click="showAlert(dessert)" flex-sm="100" flex-md="100" flex-gt-md="auto"> 
       <td>{{dessert.task}}</td> 
       <td></td> 
       <td>{{dessert.due_on}}</td> 
      </tr> 

不要分配給範圍,除非你真的需要在別處。

$scope.showAlert = function (dessert) { 
    alert('Task:' + dessert.task); 
    // if you're just using a variable in this function, declare it locally 
    var dessertType = dessert.type; 
    console.log(dessertType); 
}; 

見$指數造成問題的例子:

http://codeutopia.net/blog/2014/11/10/angularjs-best-practices-avoid-using-ng-repeats-index/comment-page-1/

相關問題