javascript - 使用 AngularJS 更改 URL 中的参数

标签 javascript angularjs

我希望单击存储在 postgresql 数据库中的客户端名称,然后将客户端的 id 值输入到 URL 中,而无需重新加载页面。这是我正在使用的带有 Angular 片段的 html:

<form class="form-inline">
        <input style="width:100%" ng-model="query" type="text" placeholder="Search Through {{clients.length}} Clients" autofocus>
        </form>
        <div class="container1">
        <div style="max-width:100%; border-width: medium; border-style: solid; border-color: gray" class="datagrid">
        <table> 
         <thead> 
          <tr> 
           <th> ID </th>
           <th> Name </th>
          </tr>
         </thead>
         <tbody>
          <tr ng-repeat="client in clients | filter: query | orderBy :'id' track by $index">
           <td>
           {{client.id}}
           </td>
           <td> 
           <a ng-href = "client/{{client.id}}"><span style="color:#0000FF"> {{client.name}}</span></a> | <a ng-href = "docgen/{{client.id}}">Letters</a> | <a ng-href = "callog/{{client.id}}">{{client.lastcall}}</a> 
           </td>
          </tr>
         </tbody>
        </table>
       </div>
       </div>
       </div>
       </div>

我不确定如何在我的 Controller 中完成此操作。我知道我可能应该使用 $location,但我不知道如何在单击特定客户名称时提取 id 值并将其附加到 url 中。另外,我并不是试图进入另一个 View ,只是动态更新 URL 参数,以便我可以使用它来过滤数据。

最佳答案

您正在使用 ui-routerng-route

无论如何,您都可以通过 $location 或 ng-href 进行移动,就像您现在所做的那样,因为 Angular 是为单页应用程序设计的。但需要在开头添加#。

<form class="form-inline">
    <input style="width:100%" ng-model="query" type="text" placeholder="Search Through {{clients.length}} Clients" autofocus>
</form>
<div class="container1">
    <div style="max-width:100%; border-width: medium; border-style: solid; border-color: gray" class="datagrid">
        <table>
            <thead>
                <tr>
                    <th> ID </th>
                    <th> Name </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="client in clients | filter: query | orderBy :'id' track by $index">
                    <td>
                        {{client.id}}
                    </td>
                    <td>
                        <a ng-href="#/client/{{client.id}} "><span style="color:#0000FF "> {{client.name}}</span></a> | <a ng-href="#/docgen/{{client.id}} ">Letters</a> | <a ng-href="#/callog/{{client.id}} ">{{client.lastcall}}</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

但是如果你坚持用 $location 来调用它,你可以这样做:

angular.module('app')
.controller('FooCtrl', function ($scope, $location) {
  $scope.goToClientDetail = function(client) {
    $location.path('/client/'+client.id);
  };
  $scope.goToDocGen = function(client) {
    $location.path('/docgen/'+client.id);
  };
  $scope.goToCallog = function(client) {
    $location.path('/callog/'+client.id);
  };
});

你的html就像

<form class="form-inline">
    <input style="width:100%" ng-model="query" type="text" placeholder="Search Through {{clients.length}} Clients" autofocus>
</form>
<div class="container1">
    <div style="max-width:100%; border-width: medium; border-style: solid; border-color: gray" class="datagrid">
        <table>
            <thead>
                <tr>
                    <th> ID </th>
                    <th> Name </th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="client in clients | filter: query | orderBy :'id' track by $index">
                    <td>
                        {{client.id}}
                    </td>
                    <td>
                        <a ng-click="goToClientDetail(client)"><span style="color:#0000FF "> {{client.name}}</span></a> | <a ng-click="goToDocGen(client)">Letters</a> | <a ng-click="goToCallog(client)">{{client.lastcall}}</a>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

关于javascript - 使用 AngularJS 更改 URL 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38983408/

相关文章:

AngularJS:如何防止/延迟应用过滤器以避免 ng-repeat 的重新排列

javascript - 选择数据清理的数组值

javascript - 如何将元素 ID 传递给函数的参数? HTML/JS

javascript - AngularJs 单元测试 - 模拟 promise 不执行 "then"

html - 带 Bootstrap 的可滚动表

javascript - Express app.post 被多次调用

javascript - Angular orderBy 字符串(数字范围)作为 Number

javascript - 通过 promise 加载 angular 1.5 组件模板

javascript - 如何在 javascript/jquery 中传递 css % 参数

javascript - Angular JS - 使用相同 ng-model 基于多个对象属性的 ng-repeat 过滤对象数组