dynamic change of templateUrl in ui-router from one state to another

独自空忆成欢 提交于 2019-12-11 02:27:16

问题


html:

<a ui-sref="user.tools.selectedTemplate({provider: t.id})" target="_blank">{{t.name}}</a>

Above code is in ng-repeat with many template links loaded with name and id so when i click each link , href will be updated with id number appended to selected template link. I am generating about ten templates with same controller. I am passing value in ui-sref from one state to another so i need dynamic templateUrl, i tried this link issue but i cant able to send stateparams since the main template page has no params.

Here the ui router code in app.js

.state('user.tools.template',angularAMD.route({
    url: '/template',
    templateUrl: './views/tools/select-template.html',
    controller: 'selectTplCtrl',
    controllerUrl: 'tools/selecttplCtrl'
}))

.state('user.tools.selectedTemplate',angularAMD.route({
    url: '/selectedTemplate/:provider',
    templateUrl: function($stateParams){
        return './views/tools/selected-template'+'$stateParams.provider'+'.html'
    },
    controller: 'selectedTemplateCtrl',
    controllerUrl: 'tools/selectedTemplateCtrl'
}))

Can anyone solve this issue ?


回答1:


I would say that you are on the right track.. just URL construction should be adjusted like this:

templateUrl: function($stateParams){
    //return './views/tools/selected-template'+'$stateParams.provider'+'.html'
    return './views/tools/selected-template'+ $stateParams.provider +'.html'
}

See more here, what we can have with UI-Router when working with dynamic template link:

  • Angular UI Router - Dynamic TemplateURL
  • Angular UI Router: decide child state template on the basis of parent resolved object
  • Angular and UI-Router, how to set a dynamic templateUrl


来源:https://stackoverflow.com/questions/38841702/dynamic-change-of-templateurl-in-ui-router-from-one-state-to-another

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