我正在学习AngularJS,并试图找出如何使用ngRepeat处理模态,控制器和范围。我找到了一个有用的JSFiddle,但我似乎无法在本地工作。
来自this帖子中提到的this JSFiddle,我有以下代码:
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="js/ui-bootstrap-tpls-2.5.0.js"></script>
<div ng-app="app">
<div ng-controller="CustomerController">
<div ng-repeat="customer in customers">
<button class="btn btn-default" ng-click="open(customer)">{{ customer.name }}</button> <br />
<!--MODAL WINDOW-->
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3>The Customer Name is: {{ customer.name }}</h3>
</div>
<div class="modal-body">
This is where the Customer Details Goes<br />
{{ customer.details }}
</div>
<div class="modal-footer">
</div>
</script>
</div>
</div>
</div>
<script>
var app = angular.module('app', ['ui.bootstrap']);
app.controller('ModalInstanceCtrl', function ($scope, $modalInstance, customer)
{
$scope.customer = customer;
});
app.controller('CustomerController', function($scope, $timeout, $modal, $log) {
$scope.customers = [
{
name: 'Ricky',
details: 'Some Details for Ricky',
},
{
name: 'Dicky',
details: 'Some Dicky Details',
},
{
name: 'Nicky',
details: 'Some Nicky Details',
}
];
// MODAL WINDOW
$scope.open = function (_customer) {
var modalInstance = $modal.open({
controller: "ModalInstanceCtrl",
templateUrl: 'myModalContent.html',
resolve: {
customer: function()
{
return _customer;
}
}
});
};
});
</script>
我的文件夹结构如下:
.
├── js
| ├── ui-bootstrap-tpls-2.5.0.js
├── css
| ├── bootstrap.min.css
├── index.html
我正在使用XAMPP打开index.html。当我这样做,没有任何加载,我得到以下错误:
angular.js:13642 Error: [$injector:unpr] http://errors.angularjs.org/1.5.6/$injector/unpr?p0=%24modalProvider%20%3C-%20%24modal%20%3C-%20CustomerController
at angular.js:38
at angular.js:4501
at Object.d [as get] (angular.js:4654)
at angular.js:4506
at d (angular.js:4654)
at e (angular.js:4678)
at Object.invoke (angular.js:4700)
at P.instance (angular.js:10177)
at n (angular.js:9096)
at g (angular.js:8459)
有谁知道为什么会发生这种情况,以及我需要在本地更改以解决此问题?
谢谢!
答案 0 :(得分:1)
阅读错误:
http://errors.angularjs.org/1.5.6/$injector/unpr?p0=%24modalProvider%20%3C-%20%24modal%20%3C-%20CustomerController
它无法在$modal
中使用CustomerController
服务,因为它是not the right one for ui-bootstrap。
您应该使用$uibModal
代替。 (它具有相同的语法: $uibModal.open()
)