How to define modal controllers in their own file?

吃可爱长大的小学妹 提交于 2020-01-03 07:31:13

问题


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'


来源:https://stackoverflow.com/questions/21840713/how-to-define-modal-controllers-in-their-own-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!