angularjsmodel-view-controllerstateui-sref

Dynamically generated templateUrl using ui-router


I want to create dynamicall generated template url usilng the id of my ng-click id. But I am getting the following error

Uncaught Error: [$injector:modulerr] Failed to instantiate module mean due to: TypeError: filename.indexOf is not a function

My router code is given below.

$stateProvider.state('file-petition-tab', {
  url: '/file-petition-tab/:id',
  templateUrl: function ($stateParams){
    return 'app/dashboard/views/tabs/tab' + $stateParams.id + '.html';
  },
  controller: 'DashboardController',
  controllerAs: 'vm',
  lazyModules: [
    'app/dashboard/dashboard.controller.js',
    'app/tpl/api.services.js',
  ]
})

My anchor tag code is

<a ui-sref="file-petition-tab({id: menu.formid})"><span>{{menu.form_code}}</span>{{menu.form_desc}}</a>

And I also want to generate dynamic controller using the id. Thanks.


Solution

  • It is working using templateProvider and $stateParams, $templateRequest.

    $stateProvider.state('file-petition-tab', {
      url: '/:id',
      templateProvider: function ($stateParams, $templateRequest) {
        var tplURL = "app/dashboard/views/tabs/tab"+$stateParams.id+".html";
        //console.log('$stateParams', $templateRequest(tplURL));
        return $templateRequest(tplURL);
      },
      controller: 'DashboardController',
      controllerAs: 'vm',
      lazyModules: [
        'app/dashboard/views/tabs/tab1.controller.js',
        'app/dashboard/dashboard.controller.js',
        'app/tpl/api.services.js',
      ],
    })
    

    and using the anchor by following format

    <a ui-sref="file-petition-tab({id: menu.formid})"><span>{{menu.form_code}}</span>{{menu.form_desc}}</a>