AngularJS 模态框弹出窗口

10

我对Angular非常陌生。我试图在此链接中重新创建模态示例https://angular-ui.github.io/bootstrap/,但我的尝试没有成功!我已经创建了一个plunkerhttp://plnkr.co/edit/018Ed7RG3Y0GoAlK7a14?p=catalogue。我只需要能够在单击按钮时打开模态框。我收到了错误消息Error: [ng:areq] Argument 'ModalDemoCtrl' is not a function, got undefined。

这是我的视图:

<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 href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ selected.item }}</b>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
    </div>
</script>
<button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
<button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
<button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
<button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>

这是我的控制器:

angular.module('crm.ma', ['ngAnimate', 'ui.bootstrap']);
angular.module('crm.ma').controller('ModalDemoCtrl', ModalDemoCtrl, function ($scope, $uibModal, $log) {

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

$scope.animationsEnabled = true;

$scope.open = function (size) {

    var modalInstance = $uibModal.open({
        animation: $scope.animationsEnabled,
        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());
    });
};

$scope.toggleAnimation = function () {
    $scope.animationsEnabled = !$scope.animationsEnabled;
};

});

angular.module('crm.ma').controller('ModalInstanceCtrl', 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');
};
});

你的 Plunkr 坏了。 - Pankaj Parkar
@PankajParkar 很抱歉,我将代码放在了index.html文件中。你现在应该能够看到它了。 - hollyquinn
在引用 script.js 之前,您需要先引用 angular.jsui-bootstrap.js - Pankaj Parkar
@PankajParkar 这只是我的应用程序的一个非常小的部分。在实际应用程序中,它们都被包含在内。 - hollyquinn
2个回答

10
这是您的plunk的修正版链接:http://plnkr.co/edit/6djuhA8ohMkrWW7zohg1?p=preview。您只是有一些小的语法错误。
var app = angular.module('crm.ma', ['ngAnimate', 'ui.bootstrap']);

app.controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {

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

$scope.animationsEnabled = true;

$scope.open = function (size) {

    var modalInstance = $uibModal.open({
        animation: $scope.animationsEnabled,
        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());
    });
};

  $scope.toggleAnimation = function () {
    $scope.animationsEnabled = !$scope.animationsEnabled;
  };

});

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

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

HTML

<!DOCTYPE html>
<html data-ng-app="crm.ma">

<head>
<link data-require="bootstrap-css@3.1.1" data-semver="3.1.1" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.14.0.js"></script>
<script src="ModalDemoCtrl.js"></script>
</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 href="#" ng-click="$event.preventDefault(); selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ selected.item }}</b>
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
        <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
      </div>
  </script>
  <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
  <button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
  <button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
  <button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>
  <div ng-show="selected">Selection from a modal: {{ selected }}</div>
  </div>
  </body>

 </html>

我修改了我的代码以匹配你的。我仍然没有收到任何错误,但是如果我将我的控制器<script src="app/components/common/ModalDemoCtrl.js"></script>添加到index.html页面中,它会返回没有错误和空白页面。你有什么想法我做错了什么吗? - hollyquinn
@hollyquinn,你解决了吗?抱歉,我因为身体原因离开了几天。 - steveo

5
您需要修复这行代码:
angular.module('crm.ma').controller('ModalDemoCtrl', ModalDemoCtrl, function ($scope, $uibModal, $log) {    
//  what is this, huh? ------------------------------------^

正确的代码:

angular.module('crm.ma').controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {

你遇到了与 ModalInstanceCtrl 相似的问题。

你还缺少 ng-app="crm.ma" 属性。

示例: http://plnkr.co/edit/VDhDAHM2beVtYYsJBXoi?p=preview


我修复了代码行,但它仍然无法工作。ng-app已包含在我的实际应用程序中。这是一个庞大的应用程序,我只是在Plunker上添加了我有疑问的页面。 - hollyquinn
引用错误:ModalInstanceCtrl未定义(匿名函数)@ ModalDemoCtrl.js:36 angular.js:68 未捕获的错误:[$injector:modulerr]无法实例化模块crm.ma,原因是: 错误:[$injector:modulerr]无法实例化模块ngAnimate,原因是: 错误:[$injector:nomod]模块“ngAnimate”不可用!您可能拼写了错误的模块名称或忘记加载它。如果要注册模块,请确保将依赖项指定为第二个参数。感谢您的帮助! - hollyquinn
这是我需要添加到index.html中使模态框工作的唯一脚本文件吗?<script src="scripts/ui-bootstrap-tpls-0.1.4.0.min.js"></script> - hollyquinn
ModalInstanceCtrl与我在答案中解释的类似。您还需要模板,或者更好的是0.14.0/ui-bootstrap-tpls.min.js文件。只需检查我发布的带有修复代码的演示即可。 - dfsq
好的,我已经去你的 Plunker 上修改了我的代码以匹配你的。我已经像你一样包含了脚本文件。但它仍然无法工作。我没有收到错误消息,而是得到了一个空白页面。如果我在我的索引页中删除 <script src="app/components/common/ModalDemoCtrl.js"></script> 的引用,那么这个空白页面就会消失。 - hollyquinn
@hollyquinn,看看我的plunk,基于你的代码。我使用了你的index.html,而不是modal.html。 - steveo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接