简体   繁体   English

使用AngularJS将URL的ID更改为特定名称

[英]Changing url's ID to a specific name using AngularJS

I'm using AngularJS for my project. 我在我的项目中使用AngularJS。 I'm creating a forum where there are many qualification levels and subjects for each qualification. 我正在创建一个论坛,每个认证有很多资格等级和主题。 Levels with type id of "F": Elementary 1, Elementary 2, Elementary 3 etc. Subjects with type id of "D": English, Maths, Science etc. All has their own unique ID and the ID of each level is linked to the parentID of Subjects. ID为“ F”的级别:初级1,初级2,初级3等。ID为“ D”的主题:英语,数学,科学等。它们都有自己的唯一ID,并且每个级别的ID都链接到主题的parentID。 When i click on Elementary 1, the id of E1 will be passed and all subjects with parentID of E1 will be displayed. 当我单击Elementary 1时,将传递E1的ID,并显示所有具有E1的parentID的主题。 So now the question, the URL of my website: http://127.0.0.1:8080/#!/discussionlist shows the forum homepage. 现在,问题出现了,我的网站URL: http : //127.0.0.1 : 8080/#!/discussionlist显示了论坛主页。 主页 When i click on Primary 1, it goes to: http://127.0.0.1:8080/#!/discussionlist/4a6f8ec4-1441-4ccc-ad4b-59fc03ca5147 当我单击Primary 1时,它将转到: http : //127.0.0.1 : 8080/#!/discussionlist/4a6f8ec4-1441-4ccc-ad4b-59fc03ca5147 第一级 When i click on English, it REPLACES the 1st id to the id of English: 当我单击英语时,它将第一个ID替换为英语的ID: 在此处输入图片说明 What i'm trying to achieve is to change the unique ID to the title of what i click on( http://127.0.0.1:8080/#!/discussionlist/Primary1 ), and to append the URL to be something like ( http://127.0.0.1:8080/#!/discussionlist/Primary1/English ). 我想要实现的是将唯一ID更改为我单击的标题( http://127.0.0.1:8080/#!/discussionlist/Primary1 ),并将URL附加为( http://127.0.0.1:8080/#!/discussionlist/Primary1/English )。 This is the code for changing the URL: 这是更改URL的代码:

    $scope.titleClicked = function (id,title){
    currentID = id
    $scope.discussiontopics.splice(0);

    console.log(id);
    $location.path("/discussionlist/" + id);


}

Route.js: Route.js:

   .when("/discussionlist", {
    controller: "discussionlistController",
    templateUrl: "src/functions/discussion-list/template.html"
})
.when("/discussionlist/:parentid", {
    controller: "discussionlistController",
    templateUrl: "src/functions/discussion-list/template.html"
})

HTML: HTML:

<div ng-controller="discussionlistController">
<div align="center" style="width: 100vw; ">
    <div class= "row content" align="left">
        <md-input-container>
            <div id="breadcrumb">
                <a href="/">Home</a> > <a href="http://127.0.0.1:8080/#!/discussionlist">Level</a> > <a href="">{{o.title}}</a> 
            </div>
            <!-- Add a short description here -->
            <div class="description">
                <p> {{ shortdescription }} </p>
            </div>
            <br />
            <!-- Add a table of contents here -->
            <div class="content-table">
                <table class="lay" md-table>
                    <tr md-row>
                        <td md-cell>Forum</td  md-cell>
                        <td  md-cell>Post</td  md-cell>
                    </tr md row>
                    <tr md-row ng-repeat="o in discussiontopics">
                        <td md-cell>
                            <a class="main-topic" ng-click="titleClicked(o.id,o.title)" aria-label="{{o.title}}">{{o.title}}</a>      
                            <p class="sub-topic">{{o.description}}</p>
                        </td md-cell>
                        <td md-cell>
                            <p></p>
                            <p class="sub-topic">{{}}</p>
                        </td md-cell>
                    </tr md-row>
                </table>
            </div>
            <div class="errormsg">
                <p>{{ error }}</p>
            </div>
        </md-input-container>   
    </div>
</div>

The thing is that there are 2 different IDs, so i have to change my id to maybe something like parentID & childID? 事实是,有2个不同的ID,所以我必须将ID更改为类似于parentID和childID之类的东西? I'm sorry I'm not that good in explaining, the gist of the question is how to change the id to the name & making it a relative URL. 抱歉,我的解释不力,问题的要点是如何将id更改为名称并使其成为相对URL。 Thank You! 谢谢!

From what I understand instead of passing your id as url param you can use what you need /english /somethingelseyouneed as url param. 据我了解,您可以将需要的/ english / somethingelseyouse用作URL参数,而不是将id作为url参数传递。 ie. 即。 you can use $stateParam to pass your id and not use it on url. 您可以使用$ stateParam传递您的ID,而不在网址上使用它。

.state(somestate.state, { url: '/path', controller: 'controller', params: { someParam: null}, controllerAs: 'vm', data: neededData } .state(somestate.state,{url:'/ path',controller:'controller',params:{someParam:null},controllerAs:'vm',data:neededData}

You can also use 'data' and pass in from state change. 您还可以使用“数据”并从状态更改中传递。 I am not sure if that helps. 我不确定这是否有帮助。 But good luck. 但是,祝你好运。

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

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