AngularJS注入Angular Bootstrap模式的问题

cyb*_*bat 15 javascript angularjs angular-ui-bootstrap

我正在整合Angular Bootstrap中的模态,并尝试将代码示例从此处调整到我的应用程序.我收到错误:Error: [$injector:unpr] Unknown provider: $modalInstanceProvider <- $modalInstance

要使$ modalInstance工作,我需要做什么?我从代码示例中看到他们已经编写了它,因此它在函数的范围内,但我不确定如何在链接控制器时编写内容.

angular.module('myApp', ['ui.bootstrap']).
controller('ModalInstanceCtrl', function($scope, $modalInstance) {
}).
factory('AuthService', ['$http', '$rootScope', '$modal',
  function($http, $rootScope, $modal) {
    return {
      loginModal: function(callback) {
        var modalInstance = $modal.open({
          templateUrl: '/partials/main/signin',
          controller: 'ModalInstanceCtrl'
        });
        modalInstance.result.then(function(selectedItem) {
          $scope.selected = selectedItem;
        }, function() {});
      }
    };
  }
]);
Run Code Online (Sandbox Code Playgroud)

cyb*_*bat 38

好的 - 问题实际上是我的模板.我已将样本中的部分修改为:

<div ng-controller="ModalInstanceCtrl">
  <div class="modal-header">
    <h3>I am a modal!</h3>
  </div>
  <div class="modal-body">
    <ul>
      <li ng-repeat="item in items">
        <a ng-click="selected.item = item">{{ item }}</a>
      </li>
    </ul>
    Selected: <b>{{ selected.item }}</b>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

实际上我需要删除ng-controller参考.

<div>
  <div class="modal-header">
    <h3>I am a modal!</h3>
  </div>
  <div class="modal-body">
    <ul>
      <li ng-repeat="item in items">
        <a ng-click="selected.item = item">{{ item }}</a>
      </li>
    </ul>
    Selected: <b>{{ selected.item }}</b>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary" ng-click="ok()">OK</button>
    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我仍然觉得我在使用Angular时磕磕绊绊,但这似乎可以解决问题!

  • 如果这有帮助我使用Angular,Angular-UI和Bootstrap 3创建了一个创建预定义模态的服务,你也可以添加你自己的模态部分和控制器来生成模态,这里是一个演示:http://codepen.io/me-conroy/pen/ALsdF你可以在gitHub上找到代码:https://github.com/me-conroy/angular-dialog-service (2认同)