How to define modal controllers in their own file?

2019-06-16 10:05发布

问题:

My Angular application has a number of modals, and I have AngularUI to provide the modal directive.

http://angular-ui.github.io/bootstrap/

So far I have several main controllers, and these are all in the app.js file as routes.

$routeProvider.when '/dashboard',         templateUrl: '/templates/dashboard/dashboard.html', controller: 'DashboardController'
$routeProvider.when '/dataset/:panel_id', templateUrl: '/templates/dataset/dataset.html',   controller: 'DatasetController'
$routeProvider.when '/batches/:panel_id', templateUrl: '/templates/design/design.html',     controller: 'PanelController'  

This works fine.

Then I created a function which calls a modal, and the modal has an instance controller of ConfigureModalCtrl. I initially had this at the bottom of the calling controller file, and it worked OK.

$scope.invokeConfigureModal = (assay_id) ->
   $scope.assay_id = assay_id
   $scope.primer3 = $scope.getPrimer(assay_id)
   modalInstance = $modal.open(
     templateUrl: '/templates/configure_modal/configure_modal.html'
     controller: ConfigureModalCtrl
     windowClass: 'configure-dialog'
     resolve:
           primer3: ->
             $scope.primer3
   )
   modalInstance.result.then ((primer3) ->
      $scope.primer3 = primer3
      return
     ), ->
      return   

Now I have moved the modal instance controller into it's own file, but the calling function cannot find it.

angular.module('assaypipelineApp').controller "ConfigureModalCtrl", ($scope,  $modalInstance, primer3) ->
  $scope.primer3 = primer3['data']
  $scope.ok = ->  
    $modalInstance.close $scope.primer3
    return
  $scope.cancel = ->
    $modalInstance.dismiss "cancel"
  serverErrorHandler = ->
    alert("There was a server error, please reload the page and try again.")

Error message

Error: Unknown provider: ConfigureModalCtrlProvider <- ConfigureModalCtrl

Similar question but does not resolve my problem. How to create separate AngularJS controller files?

So how can I resolve this problem, and why does this not occur for the other (non-modal instance) controllers? Is it because they are named in the routes?

Any recommendations on coding style are welcome also ... thanks for reading this.

回答1:

Try putting the controller as a string -- I had a similar issue.

controller: 'ConfigureModalCtrl'