如何通过选定的索引(AngularJS)在模态弹出窗口中显示数据

时间:2016-05-17 08:28:32

标签: javascript jquery angularjs

我是AngularJS的新人。如何通过选定的索引在弹出窗口中显示数据。?

这是我的代码:

<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" >
    <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Modal Header</h4>
    </div>
    <div class="modal-body" ng-repeat="product in filtered | limitTo : 1">
      <p>{{product.prod_name}} </p>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
  </div>
</div>
</div>

我的按钮:

<td><button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Detail</button> </td>

2 个答案:

答案 0 :(得分:0)

我假设网格中的所有产品行都存在详细信息按钮。 然后你可以遵循这个想法:

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click ="setValuesForPopup(prod)">

然后在你的控制器内:

$scope.setValuesForPopup = function (prod){
 $scope.product = {};
 $scope.product.prod_name=  prod.prod_name;
 //or $scope.product  = angular.copy(prod);
}

答案 1 :(得分:0)

你正在使用普通的自举角度。

最好使用角度ui-bootstrap进行角度投影。

这是ui-bootstrap的链接:http://angular-ui.github.io/bootstrap/versioned-docs/0.12.0/

使用ui-bootstrap查看以下开放模式的工作示例:

html代码:

<!doctype html>
<html ng-app="ui.bootstrap.demo">
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="ModalDemoCtrl">
    <script type="text/ng-template" id="myModalContent.html">
        <div class="modal-header">
            <h3 class="modal-title">I'm a modal!</h3>
        </div>
        <div class="modal-body">
            <ul>
                <li ng-repeat="item in items">
                    <a ng-click="selected.item = item">{{ item }}</a>
                </li>
            </ul>
            Selected: <b>{{ selected.item }}</b>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="ok()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

    <button class="btn btn-default" ng-click="open()">Open me!</button>
    <button class="btn btn-default" ng-click="open('lg')">Large modal</button>
    <button class="btn btn-default" ng-click="open('sm')">Small modal</button>
    <div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
  </body>
</html>

example.js代码:

angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function (size) {

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

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
});

// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function () {
    $modalInstance.close($scope.selected.item);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
});