Angular ModalController是它自己的文件

时间:2016-08-03 17:12:12

标签: javascript angularjs

我正在查看Angular ui modal with controller in separate js file中概述的答案和问题,我希望实现相同的结果,即在其自己的controller.js文件中有模态控制器,只是我的主控制器是在不同的方式。我就是这样的。

(function () {
     angular.module('app.mainModule')
            .controller('MainController', MainController);

    var ACTION = {
    CANCEL: 0,
    CONFIRM: 1
    };

    MainController.$inject = ['$rootScope', '$modal'];
    function MainController($rootScope, $modal) {
    var vm = this;
    vm.method1 = method1;
    vm.method2 = method2;

    function method1(){
       var modalInstance = createModal();
       // do something....
    }

  function createModal(){
        return $modal.open({
            templateUrl: 'app/someModalFile.html',
            controller: 'ModalController as vm',
            resolve: {
                action: function(){
                    return vm.action;
                },
                someVar: function() {
                    return vm.someVar.obj;
                },
                anotherVar: function(){
                    return vm.anotherVar;
                }
            }
        });
    }
 // I want to have this in a separate controller file and 
 // use the MainController to invoke this modal controller.
 function ModalController($modalInstance, someVar, anotherVar){
    var vm = this;

    vm.confirm = confirm;
    vm.cancel = cancel;
    vm.someVar = someVar;   // to display on the modal window
    vm.anotherVar = anotherVar;

    function confirm(){
        $modalInstance.close({action: ACTION.CONFIRM});
    }

    function cancel(){
        $modalInstance.close({action: ACTION.CANCEL});
    }
}
})();

知道如何实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,我认为你正在寻找这个:

http://plnkr.co/edit/lH5CMvvIEu8nqihZPfgk?p=preview

主控制器:

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function($scope, $uibModal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.animationsEnabled = true;

  $scope.open = function(size) {

  var modalInstance = $uibModal.open({
    animation: $scope.animationsEnabled,
    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());
  });
  };

  $scope.toggleAnimation = function() {
    $scope.animationsEnabled = !$scope.animationsEnabled;
  };

});

和模型控制器:

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', 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');
  };
});

答案 1 :(得分:1)

这可以通过两个简单的步骤完成:

第1步:

您需要创建一个新的控制器文件并为Modal定义控制器,就像对MainController一样:

<强>模态-controller.js

(function(){
     //you can get rid of the ModalController you have defined in the same file and rather use this
     angular.module('app.mainModule')
            .controller('ModalController', ModalController);

     ModalController.$inject = ['$modalInstance', 'someVar', AnotherVar];

     function ModalController($modalInstance, someVar, AnotherVar){
          //code here
     };

})();

Step2

您需要在 base.html (或您使用的任何文件中包含所有脚本文件)中包含此 modal-controller.js ,以便Angular知道这个控制器确实存在。