0

我正在尝试$routeProvider基于导入 json 值来实现角度动态。在这里我无法获得正确的语法,任何人都可以帮助解决这个问题。我的问题在这里我试图$routeProvider通过避免硬代码但无法将参数发送到 tempateUrl 来使其成为动态 var url="/demo1/:type", config = { templateUrl: function (params) { return "/"+route.routePathAction } } , $routeProvider.when(url,config);

$routeProvider.when('/demo1/:type', { templateUrl: function (params) { return '/demo/demo1?type=' + params.type; } })

这里

我的 _layout.cshtml

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <meta charset="utf-8" />

    <title>@ViewBag.Title</title>
    <!-- These will be in all pages -->
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/angular")

    <!-- These scripts will be from thew "Scripts" view -->
    @RenderSection("scripts", required: false)

    <!-- These styles will be in all pages -->
    <link href="~/Content/menu.css" rel="stylesheet" />
    <script src="~/Scripts/myapp.js"></script>  
    @Styles.Render("~/Content/css")
</head>

<body> 
        <h2>Index</h2>
           <ul>
            <li><a href="#/demo">Test link number "demo/:type" </a></li>
            <li><a href="#/demo1/122222234/hello">Test link number "demo/:type/:type" </a></li>
            <li><a href="#/demo2/12321323">Test link 1 number "demo/:type" </a></li>
           </ul>
        <div ng-view> </div>
    </div>

    @RenderBody()

</body>
</html>

myapp.js 文件:

var app = angular.module('myApp', ['angular.filter', 'ngRoute']);
  app.config(["$routeProvider", function ($routeProvider) {
        var setRoutes;
        var routes = [{ routePath: "demo", routePathAction: "demo/demo3" },
                      { routePath: "demo1/:type", routePathAction: "demo/demo1?type=' + params.type;" },
                      { routePath: "demo2/:type1/:type2", routePathAction: "demo/demo4?type1=' + params.type1 + '&type2=' + params.type2;" }];
            setRoutes = function (route) {
                       var config, url;
                       return url = "/" + route.routePath, config = { templateUrl: function (params) { return "/" + route.routePathAction } },                      
                       $routeProvider.when(url, config).otherwise({ redirectTo: "/" });
               }
        routes.forEach(function (route) {
                return setRoutes(route)
             })

         }]);  

我正在尝试将以下内容实现为动态

var app = angular.module('myApp', ['angular.filter', 'ngRoute']);
    app.config(["$routeProvider", function ($routeProvider) {
 $routeProvider.when('/demo', {
                        templateUrl: function (params) { return '/demo/demo3' }
                    })
                    .when('/demo1/:type', {
                        templateUrl: function (params) { return '/demo/demo1?type=' + params.type; }
                     })
                      .when('/demo1/:type1/:type2', {
                          templateUrl: function (params) { return '/demo/demo4?type1=' + params.type1 + '&type2=' + params.type2; }
                    }).otherwise({ redirectTo: "/" })
4

0 回答 0