【问题标题】:AngularJS Modal PopupAngularJS 模态弹出窗口
【发布时间】:2015-10-09 20:06:51
【问题描述】:

我对 Angular 真的很陌生。我正在尝试在此链接https://angular-ui.github.io/bootstrap/ 上重新创建模态示例,但我没有运气!我创建了一个 plunker http://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 坏了。
  • @PankajParkar 对此感到抱歉。我将代码放在 index.html 文件中。您现在应该可以看到它了。
  • 你需要在script.js之前包含angular.js &amp; ui-bootstrap.js
  • @PankajParkar 这是我应用程序的一小部分。在实际应用中,它们都包含在内。

标签: angularjs twitter-bootstrap bootstrap-modal


【解决方案1】:

这是您的 plunk 的更正分支:http://plnkr.co/edit/6djuhA8ohMkrWW7zohg1?p=preview。 你只是有一些小的语法错误。

JAVASCRIPT

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>

【讨论】:

  • 我更改了代码以匹配您的代码。我仍然没有收到任何错误,但是如果我将控制器 添加到 index.html 页面,它会返回没有错误和空白页。知道我做错了什么吗?
  • @hollyquinn,你搞定了吗?抱歉,我出去看病几天了。
【解决方案2】:

你需要修复这条线:

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 上添加了我有疑问的页面。
  • ReferenceError: ModalInstanceCtrl is not defined(anonymous function) @ ModalDemoCtrl.js:36 angular.js:68 Uncaught Error: [$injector:modulerr] 无法实例化模块 crm.ma 由于:错误: [$injector:modulerr] 无法实例化模块 ngAnimate,原因是:错误:[$injector:nomod] 模块 'ngAnimate' 不可用!您要么拼错了模块名称,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。感谢您的帮助!
  • 这是我需要添加到 index.html 以使模式正常工作的唯一脚本文件吗?
  • ModalInstanceCtrl 与我在答案中解释的类似。您还需要模板,或者更好的 0.14.0/ui-bootstrap-tpls.min.js 文件。只需查看我发布的带有固定代码的演示。
  • 好的,我去了你的 plunker 并更改了我的代码以匹配你的。我已经像你一样包含了脚本文件。它仍然不起作用。我没有收到错误消息,我得到一个空白页。如果我删除索引页中的 引用,空白页就会消失。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多