尝试基于常量在控制器中动态设置templateUrl

时间:2015-01-05 06:13:00

标签: angularjs angular-ui-router

我想根据我在angularjs bootstrap中定义的预设常量来更改与控制器关联的templateUrl。我无法弄清楚如何改变它。我已经尝试过UrlRouteProvider但是还没弄清楚如何从文件系统中拉出html。我被困在templateUrl上。

在下面的代码中,输出首先显示“svcc确实传递到第一个函数的console.out但是在templateUrl定义函数中,CONFIG未定义。

我愿意采取其他方式来做到这一点。

    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) {
                    }
                });
        }]);

3 个答案:

答案 0 :(得分:5)

我创建了plunker here 你几乎就在那里,只是语法是 'templateProvider'

.state('home', {
    url: '/home',
    //templateUrl: 'index5templateA.html',   (THIS WORKS)
    // templateUrl: function(CONFIG) {
    templateProvider: function(CONFIG) {
    ...
来自doc的

片段:

Templates

  

<强> TemplateUrl
  ... templateUrl也可以是返回网址的函数。 需要一个预设参数stateParams,它不会被注入。

     

<强> TemplateProvider
  或者您可以使用模板提供程序功能,可以注入,可以访问本地,并且必须返回模板HTML,如下所示:

所以在我们的例子中,那将是实现:

 $stateProvider
    .state('home', {
        url: '/home',
        //templateUrl: 'index5templateA.html',   (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

同时检查:

答案 1 :(得分:3)

我必须添加另一个答案,与角1.3的全新功能相关

$templateRequest

  

$templateRequest服务使用$http下载提供的模板,并在成功时将内容存储在$templateCache内。

<强>用法

$templateRequest(tpl, [ignoreRequestError]);

<强>参数 - tpl string - HTTP请求模板URL

  • ignoreRequestError(可选)boolean - 当请求失败或模板为空时是否忽略异常

updated plunker

templateProvider: function(CONFIG, $templateRequest) {
    console.log('in templateUrl ' + CONFIG.codeCampType);

    var templateName = 'index5templateB.html';

    if (CONFIG.codeCampType === "svcc") {
         templateName = 'index5templateA.html';
    } 

    return $templateRequest(templateName);
},

答案 2 :(得分:2)

 var app = angular.module('svccApp', [
        'ui.router'
    ]);

    var myConstant = {};
    myConstant.codeCampType = "svcc";

    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() {
                        if (myConstant.codeCampType === "svcc") {
                            return 'index5templateA.html';
                        } else {
                            return 'index5templateB.html';
                        }
                    },
                    controller: function ($state) {
                    }
                });
        }]);