Angular 模态服务

Angular Modal Service

我正在尝试让模式在我的 Angular 应用程序中工作。我尝试使用 Bootstrap 和 Angular Material 模态,但我无法使它们工作。我决定尝试使用 Angular 模式服务,但是我遇到了同样的错误。每当我调用函数 ModalService.showModal 时,我都会收到以下错误:

angular.js:12416 TypeError: Cannot read property 'showModal' of undefined
at Scope.$scope.show (AboutController.js:4)
at fn (eval at <anonymous> (angular.js:13231), <anonymous>:4:203)
at callback (angular.js:23371)
at Scope.$eval (angular.js:15878)
at Scope.$apply (angular.js:15978)
at HTMLAnchorElement.<anonymous> (angular.js:23376)
at HTMLAnchorElement.n.event.dispatch (jquery-2.2.3.min.js:3)
at HTMLAnchorElement.r.handle (jquery-2.2.3.min.js:3)

我正在使用的服务:

https://github.com/dwmkerr/angular-modal-service

我用的JS Fiddle例子:

http://jsfiddle.net/dwmkerr/8MVLJ/?utm_source=website&utm_medium=embed&utm_campaign=8MVLJ

包括:

<!-- Include jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<!-- Include Bootstrap -->
<script src="../node_modules/bootstrap/js/bootstrap.js"></script>
<!-- Include Angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.js"></script>
<!-- Include ModalService-->
<script src="../node_modules/angular-modal-service/dst/angular-modal-service.min.js"></script> 

About.html:

<div class="container">
<div class="col-sm-6 col-sm-offset-3 form-box">

<h3>Comentarios</h3>

<a class="btn btn-default" href ng-click="show()">Show a Modal</a>
 <!-- The actual modal template, just a bit o bootstrap -->
 <script type="text/ng-template" id="modal.html">
     <div class="modal fade">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" ng-click="close('Cancel')" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Yes or No?</h4>
          </div>
          <div class="modal-body">
            <p>It's your call...</p>
          </div>
          <div class="modal-footer">
            <button type="button" ng-click="close('No')" class="btn btn-default" data-dismiss="modal">No</button>
            <button type="button" ng-click="close('Yes')" class="btn btn-primary" data-dismiss="modal">Yes</button>
          </div>
        </div>
      </div>
    </div>
 </script>
</div>
</div>

App.module.js:

var Tiendiia = angular.module('Tiendiia', ['ui.router', 'ngMaterial', 'ui.bootstrap', 'ngCookies', 'angularModalService',]);

Tiendiia.config(function($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('yellow')
.accentPalette('orange')
.warnPalette('amber')
.backgroundPalette('grey');
});

AboutController.js

Tiendiia.controller('AboutController', ['$scope',  function($scope, ModalService) {

$scope.show = function() {
    ModalService.showModal({
        templateUrl: 'modal.html',
        controller: "ModalController"
    }).then(function(modal) {
        modal.element.modal();
        modal.close.then(function(result) {
            $scope.message = "You said " + result;
        });
    });
};


}]);

ModalController.js

Tiendiia.controller('ModalController', function($scope, close) {

 $scope.close = function(result) {
close(result, 500); // close, but give 500ms for bootstrap to animate
};

});

我已经尽我所能地查看了所有地方,但我找不到它不起作用的原因。

谢谢!

在 angular material 上,它被称为您需要的对话框。有时 angular material 使用推荐的 John Papa 风格,但有时初学者很难理解。懂了这个guide就真的懂了angular.

Link To styleguide

尝试使用 ui.bootstrap 的模式,不要添加不必要的库,这些库具有您在 ui bootstrap 上已有的功能。不管怎样,我认为你的错误可能在这里:

好像在关于控制器

['$scope', function($scope, ModalService)

您需要添加ModalService

['$scope', 'ModalService', function($scope, ModalService)