角度ng重复传递项以与父数据一起使用

Angular ng-repeat pass item to function with parent data?

本文关键字:数据 一起 ng 角度      更新时间:2023-09-26

我想将 {{trade}} 及其父级传递给 open 函数。

当我点击 li 时,我希望父(股票)数据和子(交易)数据显示在警报框中。

这是 Plunkr: http://plnkr.co/edit/QuG8NcOvPKVAnfRNYwlx?p=preview

.html:

<table ng-controller = "TickerListCtrl">
<thead>
  <th>Symbol</th>
</thead>
<tbody ng-repeat="ticker in tickers">
  <tr ng-click="showTrades(ticker)">
      <td>{{ticker.Ticker}}</td>
  </tr>
  <tr ng-show="ticker.showTrades">
      <td colspan="2">
        <ul ng-repeat="trade in ticker.trades">
          <li ng-click="open(trade)">{{trade}}</li>
        </ul>
      </td>
  </tr>
 </tbody>
</table> 

股票代码:

  $scope.tickers = [
{
    "Ticker": "4512",
    "TradeCount": "91",
    "trades" :
      [{
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      },
      {
        "one": "this one",
        "two": "this two",
        "three": "this three"
      }]
}]
$scope.open = function (trade) {
   alert(ticker.Ticker);
};

替换为 :

 $scope.open = function (ticker, trade) {
  var myObj = {};
  myObj['Ticker'] = ticker['Ticker'];
  myObj['TradeCount'] = ticker['TradeCount'];
  delete trade['$$hashKey'];
  myObj['trades'] = trade;
 alert(JSON.stringify(myObj));

};

演示