Load Controller based on state params using Angular ui-router

余生长醉 提交于 2019-12-06 13:57:19

What is happening here is that when you write this:

controller: 'NemtCtrl'

You tell angular to get the controller named 'NemtCtrl'. But when you on the other hand write this:

controller: 
   function ($stateParams) {
        return 'NemtCtrl';
   }

you are defining a controller for that state.

Update

According to the ui-router docs the way to do is as follows:

$stateProvider.state('contacts', {
  template: ...,
  controllerProvider: function($stateParams) {
      var ctrlName = $stateParams.type + "Controller";
      return ctrlName;
  }
})

You can read more about it here

Update 2

For your case it would be something like:

.state("dashboard.item.detail", {
  url: "/detailId/:detailId/detailName/:detailName",
  views: {

    'main@': {
      templateUrl:
        function ($stateParams){
          //move this to a util function later
          var tempName = unescape($stateParams.detailName);
          tempName = tempName.replace(/\s/g, "-");

          return '../partials/slides/' + tempName + '.html';
        },
      resolve: {
        DetailData: ['DetailService',
          function(DetailService){
            return DetailService.getDetails();
          }]
      },
      controllerProvider: //Change to controllerProvider instead of controller
        function ($stateParams) {
          //console.log( $stateParams.detailName + 'Ctrl');
          return $stateParams.detailName + 'Ctrl';
        }
    }

  }

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