javascript - Angular UI Router-在 url 中插入参数

标签 javascript angularjs angularjs-scope angular-ui-router angularjs-ng-repeat

正在开发一个项目,我需要显示 url 链接并根据我在 url 中传递的 param/id 呈现数据。在以下链接中,AWS 是 id。

链接: http://link.com/Inventory/ListInventory/TID/AWS/JSON

链接: http://link.com/Inventory/ListInventory/TID/param/JSON

enter image description here

目前,数据根据我从下拉列表中选择的 ID 进行渲染,但 url 不会更改。我下面有一些代码,但不确定我到底做错了什么。

应用程序:

define app
routes
$stateProvider
        .state('table', 
            {
                url: '/table',
                templateUrl: './js/views/tableTmpl.html',
                controller: 'tableCtrl'
            })
            .state('table.test', 
                {
                    url: '/test/:tid',
                    templateUrl: './js/views/tableTmpl.html',
                    controller: 'tableCtrl'
            });

Controller

(function () {
var app = angular.module('inventory_app');

app.controller('tableCtrl', function($scope, tableService, uiGridConstants, $stateParams, $state) {

    /************************************************
    GLOBAL TEAM ID VALUES
    ************************************************/
    $scope.tids = ['AWS', 'RET', 'DO', 'HELLO', 'HG', 'MIM', 'ALL'];
    var globalteamId = 'AWS';

    $scope.tidsIdFunc = function(tidId) {
        globalteamId = tidId;

        $scope.itemClick = function(tid){
          $state.location('table.test', {'ID': tid})
        }; 

        /*Pass in argumnet for the functions below*/
        getCost(globalteamId);
        getAllData(globalteamId);
        pieGraph(globalteamId);
        histoGraph(globalteamId);
        lineGraph(globalteamId);
    };

查看

<div class="dropdown">
      <button class="btn btn-primary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
        TID ID
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
        <li ng-repeat ="tid in tids">
          <!-- <a ng-click="tidsIdFunc(tid)">{{tid}}</a> -->
          <a ng-click="itemClick(tid)">{{tid}}</a>
        </li>
      </ul>
    </div>
  </div>
</div>

服务 对 json 进行 HTTP 调用

最佳答案

您需要在单击项目时导航到其他状态,因此最好通过在某个位置使用 ui-sref 指令来简单化

<a ui-sref="table.test({tid: tid})">{{tid}}</a>
<小时/>

真正的问题是您调用了 $state API 的错误方法来浏览页面。

应该是

$state.go('table.test', {'tid': tid})

而不是

$state.location('table.test', {'ID': tid})

关于javascript - Angular UI Router-在 url 中插入参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070121/

相关文章:

javascript - 无法将 JSON 响应数据分配给 AngularJS 中的 $scope 变量

javascript - 与隔离范围指令的绑定(bind)有时不在范围内

javascript - 通过 PHP 或 Java 显示 HTML 源代码

javascript - 具有动态页面数的 C# ASP.NET 服务器端轮播 slider

javascript - 您可以将 Angular 服务作为参数传递给另一个 Angular 服务提供的函数吗?

css - 使用 angular 1.5 $scope 增量更改 CSS 背景颜色的语法

javascript - jquery 如何让这个对话框确定按钮工作?

javascript : In For loop pushing the value into the array variable, 但值被附加(推送)为未定义

javascript - AngularJS Controller 在服务之前执行 console.log()

javascript - 如何在 AngularJS 中 Controller 之间进行通信?