AngularJS模态弹出窗口

编程入门 行业动态 更新时间:2024-10-27 09:34:51
本文介绍了AngularJS模态弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我真的是Angular的新手.我正在尝试在此链接 angular-ui.github.io/bootstrap/我没有运气!我创建了一个监听器 plnkr.co/edit/018Ed7RG3Y0GoAlK7a14?p=catalogue 能够在单击按钮时打开模态.我收到错误消息错误:[ng:areq]参数'ModalDemoCtrl'不是一个函数,未定义

I'm really new to Angular. I'm trying to recreate the modal sample at this link angular-ui.github.io/bootstrap/ I am having no luck with it! I created a plunker plnkr.co/edit/018Ed7RG3Y0GoAlK7a14?p=catalogue I just need to be able to open a modal on a button click. I'm getting the error message 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'); }; });

推荐答案

这是您的pl子的更正叉: plnkr.co/edit/6djuhA8ohMkrWW7zohg1?p=preview . 您只是有一些小语法错误.

Here's a corrected fork of your plunk: plnkr.co/edit/6djuhA8ohMkrWW7zohg1?p=preview. You just had some minor syntax errors.

JAVASCRIPT

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/bootstrap/3.1.1/css/bootstrap.min.css" /> <link rel="stylesheet" href="style.css" /> <script src="//ajax.googleapis/ajax/libs/angularjs/1.4.6/angular.js"></script> <script src="//ajax.googleapis/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>

更多推荐

AngularJS模态弹出窗口

本文发布于:2023-10-06 19:35:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出窗口   模态   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!