【问题标题】:Passing a row of data from a table to a modal with Angular使用Angular将一行数据从表传递到模态
【发布时间】:2015-06-28 18:06:21
【问题描述】:

我有一个显示竞赛条目的表格(使用 PHP 从数据库中提取并转换为 .json 对象以与 AngularJS 和 JavaScript 一起使用)。我还想在上面实现一个模态,所以当“法官”点击每个条目时,他们可以看到该条目的详细信息。所以基本上,我需要将一行数据传递给该模式(一个 ui.bootstrap 模式)。

这是包含所有数据的表的标记。模态应用于 ng-repeated :

<table class="submissions">

<tr class="tb-header">
<td>id</td>
<td>wa #</td>
<td>name</td>
<td>email</td>
<td>file</td>
<td>rating</td>
<td>submitted on</td></tr>

    <tr ng-click="open()" ng-repeat="row in rows track by $index">

        <td>{{ row.id }}</td>
        <td class="wa-num">{{ row.wa_num }}</td>
        <td>{{ row.name }}</td>
        <td>{{ row.email }}</td>
        <td id="submitted-file">{{ row.file }}</td>
        <td>{{ row.rating }}</td>
        <td>{{ row.submitted }}</td>

    </tr>


</table>

这是控制整个页面和模式的控制器:

.controller('dashboard',['$scope', '$rootScope', '$location', '$modal', 'loginService', 'getEntries',
          function($scope, $rootScope, $location, $modal, loginService, getEntries){

          $scope.open = function () {

              var modalInstance = $modal.open({
                  templateUrl: '/partials/submission_mod.html',
                  controller: ['$scope', '$modalInstance', function($scope, $modalInstance){
                      $scope.modalInstance = $modalInstance;

                      $scope.cats = "Submission info goes here.";
                  }]
              });
          };

          var entries = getEntries.entries();

              entries.save(
                  function(result){
                      console.log(result);

                      //$scope.rows = [];
                      $scope.rows = result.entries;
                      console.log($scope.rows);


                  },
                  function(result) {
                      console.log(result);
                  }

              );
    }])

这是模态的标记(由于某种原因,它目前没有引入任何东西,甚至没有硬编码的“猫”):

<div class="modal-entry">{{ cats }}</div>
<button class="btn btn-primary btn-modal" ng-click="modalInstance.close()">Close</button>

问题是:如何将数据传递给该模式?如何定位它,使其仅拉动被点击的行等?

非常感谢任何指导。

【问题讨论】:

    标签: javascript angularjs ui.bootstrap


    【解决方案1】:

    plinkr 有一些关于如何从Angular Bootstrap Directives Docs 做到这一点的信息

    像这样的解决方法:

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });
    

    【讨论】:

    • @varvara_jones 如果这是正确答案,请接受。如果不让我知道,我可能会提供帮助。
    【解决方案2】:

    最好的方法是将行作为参数传递给您的模态函数。

    <tr ng-click="open(row)" ng-repeat="row in rows track by $index">
      ...
    </tr>
    

    并在您的功能中,接收该行:

    $scope.openModal = function (row) {
        var modalInstance = $modal.open({
            templateUrl: '/partials/submission_mod.html',
            controller: 'ModalCtrl',
            resolve: {
                cat: function () {
                    return row;
                }
            }
        });
    };
    

    然后传递给你的控制器:

    .controller('ModalCtrl', function ($scope, $modalInstance, cat) {
        $scope.cat = cat;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2021-10-22
      • 2020-10-23
      • 2021-11-14
      • 2021-11-11
      相关资源
      最近更新 更多