AngularJS - 使用routeProvider"当"变量构建templateUrl的名字吗? [英] AngularJS - use routeProvider "when" variables to construct templateUrl name?

查看:173
本文介绍了AngularJS - 使用routeProvider"当"变量构建templateUrl的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这就是我要完成的:

So this is what I am trying to accomplish:

'use strict';

var app = angular.module('myModule', ['ngRoute']);

app.config(function($routeProvider) {
  $routeProvider
    .when('/', {
      redirectTo: '/pages'
    })
    .when('/pages/:pageName', {
      templateUrl: 'views/pages/'+pageName+'html',
      controller: 'MainController'
    });
});

基本上,我想使用URI来决定使用哪个模板。目前,我得到的页面名称没有定义我的理解错误。什么是这样做的不同的方式?
谢谢!

Basically, I want to use the uri to determine which template is used. Currently I get an error that pageName is not defined which I understand. What would be a different way of doing this? Thanks!

推荐答案

templateUrl 可路由参数的函数接受对象:

templateUrl can be a function accepting object of route parameters:

.when('/pages/:pageName', {
    templateUrl: function(params) {
        return 'views/pages/' + params.pageName + '.html';
    },
    controller: 'MainController'
});

这篇关于AngularJS - 使用routeProvider"当"变量构建templateUrl的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆