使用 UI-Bootstrap 模态与 Angular-Fullstack 和 ES6

Use UI-Bootstrap Modal with Angular-Fullstack and ES6

我想使用 ui-bootsrap Modal with the AngularJS Fullstack Generator & ES6,但它不起作用。 我想选择一个项目,单击 "Edit" 并在大模态中编辑项目。但是我没有打开模式。

在控制台中,我收到此错误消息:

"Error: $modal is not defined"

我的项目控制器看起来像这样:

'use strict';

(function() {

  class ProjectCtrl {

    constructor(Project, $modal, $log) {
      this.project = Project;
      this.projects = [];
      this.getAllProjects(Project);
      this.$modal = $modal;
      this.log = $log;
    }

    // Open a modal window to Update a single Project
    modalUpdate(size, selectedProject) {
      var modalInstance = $modal.open({
        templateUrl: 'app/project/project-edit.modal.html',
        controller: function ($scope, modalInstance, aProject) {
          $scope.project = aProject;
        },
        size: size,
        resolve: {
          aProject: function () {
            return selectedProject;
          }
        }
      });

      modalInstance.result.then(function (selectedItem) {
        this.selected = selectedItem;
      }, function () {
        $log.info('Modal dismissed at: ' + new Date());
      });
    };
  }

angular.module('projectApp')
    .controller('ProjectCtrl', ProjectCtrl);
})();

app.js 看起来像这样:

'use strict';

angular.module('projectApp', [
  'projectApp.constants',
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ui.router',
  'ui.bootstrap' 
])
  .config(function($urlRouterProvider, $locationProvider) {
    $urlRouterProvider
      .otherwise('/');

    $locationProvider.html5Mode(true);
  });

打开模态框的按钮:

<button type="button" class="btn btn-default btn-xs pull-right" ng-click="ProjectCtrl.modalUpdate('lg', project)"><span aria-hidden="false"></span> Edit</button>

我的猜测是,在 ProjectCtrl 中,我必须以某种方式 $inject 'Project'、'$modal' 和 '$log',但我不知道如何以及在哪里。

您使用的 ui-bootstrap 是哪个版本?

语法因您使用的版本而异。

假设您使用的是 v0.13.4 或更早版本 并且没有使用 ES6,这里是解决方案:

注意: 如果您使用的 比 v0.13.4 更新的版本,则将 $modal 替换为 $uibModal 并将 $modalInstance 替换为 $uibModalInstance 并且它应该也工作。


首先,你需要做的是在命令行中写入:

yo angular-fullstack:controller MyModalName

然后在 myModalName.controller.js 中输入:

.controller('MyModalNameCtrl', function ($scope,$modal) {

$scope.openModal = function () {
  var modalInstance = $modal.open({
    animation: true,
    templateUrl: 'testmodal',
    controller: 'ModalInstanceCtrl',

  });

  modalInstance.result.then(function () {}, function () {
    console.log('Modal dismissed at: ' + new Date());
  });
};

});

然后还是在myModalName.controller.js下面放:

//change theNameOfYourApp to your corresponding app name !
angular.module('theNameOfYourApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance) {

  $scope.ok = function () {
    $modalInstance.close(); 
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
});

然后最后一步,在你的 .html 页面 中你想要显示模式的地方,输入:

<!--Modal Template-->
<div ng-controller="MyModalNameCtrl">
  <script type="text/ng-template" id="testmodal">
    <div class="modal-header">
      <h3 class="modal-title">Hello :</h3>
    </div>
    <div class="modal-body">
      <p>hey, this is the body </p>
    </div>
    <div class="modal-footer">
      <button class="btn btn-default" type="button" ng-click="ok()">OK</button>

      <button class="btn btn-default" type="button" ng-click="cancel()">Cancel()</button>
    </div>
  </script>
</div>

我用另一种方式解决了 Modal 问题,因为 UI Bootstrap 似乎在 ES6 中不起作用。至少现在不会。

我为 Angular-Fullstack 使用了模态服务 here

这是一个带有删除模式的简单示例:

'use strict';

(function() {

class AdminController {
  constructor(User, Modal) {
    // Use the User $resource to fetch all users
    this.users = User.query();
    this.modal = Modal;
  }

  delete(user) {
    var deleteConfirmationModal = this.modal.confirm.delete((user) => {
        user.$remove();
        this.users.splice(this.users.indexOf(user), 1);
    });

    deleteConfirmationModal(user.name, user);
  }
}

angular.module('sampleApp.admin')
  .controller('AdminController', AdminController);

})();

模态框是这样的: