【问题标题】:How to redirect to a different template based on the scope in AngularJS?如何根据AngularJS中的范围重定向到不同的模板?
【发布时间】:2014-09-21 21:04:02
【问题描述】:

我想根据我的范围使用不同的模板。

目前,我只有一个名为:view-project.html 的模板可通过此网址访问:#/project/:id

现在,我需要两个模板:view-project-1.htmlview-project-2.html。 我需要他们保持相同的网址:#/project/:id

例如,如果我的 $scope.template = 1,我想使用 view-project-1.html 和这个 url #/project/300。如果$scope.template = 2 我想使用view-project-2.html 这个url #/project/300

有可能这样做吗?

【问题讨论】:

标签: angularjs angularjs-scope angular-template


【解决方案1】:
app.config(function($routeProvider) {
    $routeProvider
        .when('/project/:id', {
            templateUrl: 'template.html',
            controller: 'projectcontroller'
        })
});

然后在你的控制器中:

app.controller('projectcontroller', function($scope) {
    $scope.template = 2;
});

模板.html:

<div ng-include="template==2?'view-project-2.html':'view-project-1.html'"></div>

DEMO PLUNKER

【讨论】:

    【解决方案2】:

    在我看来,这可以通过以下方式完成:

    1. 使用 ng-include 将两个模板导入同一个文件中。

    2. 您可以使用 ng-switch 或 ng-if 根据路由参数中的值呈现两个模板之一。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      • 2012-07-02
      • 2015-08-01
      相关资源
      最近更新 更多