大佬教程收集整理的这篇文章主要介绍了angularjs – 尝试基于常量在控制器中动态设置templateUrl,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在下面的代码中,输出首先显示“svcc确实传递到第一个函数的console.out但是在templateUrl定义函数中,CONFIG是未定义的.@H_696_2@
我愿意采取其他方式来做到这一点.@H_696_2@
var app = angular.module('svccApp',[ 'ui.router' ]); var myConstant = {}; myConstant.codeCampType = "svcc"; app.constant("CONFIG",myConstant); app.config(['$stateProvider','$urlRouterProvider','CONFIG',function ($stateProvider,$urlRouterProvider,CONFIG) { console.log(CONFIG.codeCampTypE); $stateProvider .state('home',{ url: '/home',//templateUrl: 'index5templateA.html',(THIS WORKS) templateUrl: function(CONFIG) { console.log('in templateUrl ' + CONFIG.codeCampTypE); if (CONFIG.codeCampType === "svcc") { return 'index5templateA.html'; } else { return 'index5templateB.html'; } },controller: function ($statE) { } }); }]);
.state('home',{ url: '/home',(THIS WORKS) // templateUrl: function(CONFIG) { templateProvider: function(CONFIG) { ...
来自doc的片段:@H_696_2@
所以在我们的例子中,那将是实现:@H_696_2@
$stateProvider .state('home',{ url: '/home',(THIS WORKS) templateProvider: function(CONFIG,$http,$templateCachE) { console.log('in templateUrl ' + CONFIG.codeCampTypE); var templatename = 'index5templateB.html'; if (CONFIG.codeCampType === "svcc") { templatename = 'index5templateA.html'; } var tpl = $templateCache.get(templateName); if(tpl){ return tpl; } return $http .get(templateName) .then(function(responsE){ tpl = response.data $templateCache.put(templatename,tpl); return tpl; }); },controller: function ($statE) { } });
检查examle here@H_696_2@
还检查:@H_696_2@
> Angular UI Router: decide child state template on the basis of parent resolved object
> Angular and UI-Router,how to set a dynamic templateUrl@H_696_2@
以上是大佬教程为你收集整理的angularjs – 尝试基于常量在控制器中动态设置templateUrl全部内容,希望文章能够帮你解决angularjs – 尝试基于常量在控制器中动态设置templateUrl所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。