如何从角度控制器打开模态

时间:2014-07-18 01:00:16

标签: javascript angularjs twitter-bootstrap

我有一个角度控制器,想要在我的视图中打开一个模态。

我的HTML

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

  <div class="modal-dialog">

    <div class="modal-content">

      <div class="modal-header">

        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>

        <h4 class="modal-title" id="myModalLabel">Modal title</h4>

      </div>

      <div class="modal-body">

        ...

      </div>

      <div class="modal-footer">

        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

        <button type="button" class="btn btn-primary">Save changes</button>

      </div>

    </div>

  </div>

</div>


Settings Controller

function SettingsController($scope,WalletManager,Storage){


    $scope.pageClass = 'page-settings';


    ];



 $scope.myModal = function () {

        var modalInstance = $myModal.open({


        });

如何从控制器打开?

2 个答案:

答案 0 :(得分:7)

根据Soren的建议,UI bootstrap是可行的方法。以下是他们通过AngularJS打开模态的文档中的示例。

并使用以下代码向他们的Plunker link

<html>
<head>

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.js"></script>
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script>
    <script src="example.js"></script>
    <link href=
    "//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel=
    "stylesheet">

    <title></title>
</head>

<body>
    <div>
        <script id="myModalContent.html" type="text/ng-template">
<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">Open me!</button>
        <button class="btn btn-default">Large modal</button> <button class=
        "btn btn-default">Small modal</button>

        <div>
            Selection from a modal: {{ selected }}
        </div>
    </div>
</body>
</html>

而且,javascript:

angular.module('plunker', ['ui.bootstrap']);
var 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());
    });
  };
};

var 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');
  };
};

答案 1 :(得分:2)

看一下所有模态的angular-bootstrap,再加上一些有趣的效果,可以用凉亭安装。

该模块附带了一些示例,向您展示如何从控制器中调用它。