请找到我的以下代码,
.js文件
var app = angular.module("rgMenu", ['ui.bootstrap']);
app.controller("MenuController",
function ($scope, $http) {
///Some code here
});
以上一切正常,但我想使用$modal
,所以我使用下面的代码
.html文件
src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.4.js"
.js文件
var app = angular.module("rgMenu", ['ui.bootstrap']);
app.controller("MenuController",
function ($scope, $http, $modal) {
///Some code here
});
当我在控制器中添加$modal
时,我的旧代码失败,它将无效。我有水平菜单,它将无法正常工作。
是否无法在同一控制器中编写$modal
?如何在上面的代码中使用$modal
?
答案 0 :(得分:0)
这是你的解决方案:
<强> myModalContent.html 强>
<div>
<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>
</div>
<强>的index.html 强>
<!DOCTYPE html>
<html lang="en" ng-app="demo">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js" ></script>
<script src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
</head>
<body>
<div ng-controller="MenuController">
<div class="text-center" style="margin-top:5%;">
<button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
<button type="button" class="btn btn-default" ng-click="open('sm')">Small modal</button>
<button type="button" class="btn btn-default" ng-click="open('lg')">Large modal</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
</div>
<script>
angular.module('demo', ['ui.bootstrap'])
.controller('MenuController', ['$scope', '$uibModal', '$log', function ($scope, $uibModal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function (size) {
var modalInstance = $uibModal.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());
});
};
}])
.controller('ModalInstanceCtrl', ['$scope', '$uibModalInstance', 'items', function ($scope, $uibModalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function () {
$uibModalInstance.close($scope.selected.item);
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
}]);
</script>
</body>
</html>