简体   繁体   English

带有动态网址的角度内联模板

[英]Inline template in angular with dynamic url

Hi I want to use a similar functionality like : Using Inline Templates in Angular 嗨,我想使用类似的功能,例如: 在Angular中使用内联模板

But in my case url will be dynamic. 但就我而言,URL是动态的。 for example in below code url will have /first or /second but in my case it will have the category id. 例如,在下面的代码中,URL将具有/ first或/ second,但就我而言,它将具有类别ID。 there can be 100 of category so I could not write 100 condition 可以有100个类别,所以我不能写100个条件

  learningApp.config(function ($routeProvider) {$routeProvider.
    when("/cat1",{ controller: "SimpleController", templateUrl: "temp1.html"}).
    when("/cat2", {controller: "SimpleController", templateUrl: "temp2.html"}).
    otherwise({redirectTo : "/first"});

}); });

<script type="text/ng-template" id="cat1.html">
<div class="view">
    <h2>First View</h2>
    <p>
        Search:<input type="text" ng-model="filterText" />
    </p>
    <ul class="nav nav-pills">
        <li ng-repeat="cust in customers | orderBy:'name' | filter: filterText "><a href="#">{{cust.name}} - {{cust.school}}</a></li>
    </ul>
</div>
</script>

<script type="text/ng-template" id="cat2.html">
<div class="view">
    <h2>Second View</h2>
    <p>
       Search:<input type="text" ng-model="filterText" />
    </p>
    <ul class="nav nav-pills">
        <li ng-repeat="cust in customers | orderBy:'name' | filter:    filterText "><a href= "#">{{cust.name}} - {{cust.school}}</a></li>
    </ul>
</div>

and definitely I will have a section with id as category id corresponding to each category. 当然,我将有一个ID为对应于每个类别的类别ID的部分。

You can specify Your URL like this: 您可以这样指定您的URL:

.when("/category/:catId") {
    controller: "ControllerName",
    templateUrl: "templateURL",
}

And inside your controller: 在您的控制器内部:

.controller('ControllerName', ['$scope', '$routeParams', function($scope, $routeParams) {
    var $scope.categoryFromRoute = $routeParams.catId;
}]);

Eg - Navigating to "/category/1" will lead to $routeParams.catId having the value equal to 1, inside the controller. 例如-导航到“ / category / 1”将导致$routeParams.catId在控制器内部具有等于1的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM