在ui路由中传递动态参数值

时间:2019-01-03 05:43:10

标签: javascript angularjs angular-ui-router

我正在使用UI路由器 但我无法动态传递价值 如何在ui路由中动态传递searchName

.state("search", {
  name: 'search',
  url: '/search/{search}',
  templateUrl: "search-page",
  controller: "searchController"
})


myApp.controller('searchController', function($scope, $http, $stateParams) {
  $scope.searchName = $stateParams.search;
  console.log($scope.searchName);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div class="input-group">
  <input type="text" class="form-control" placeholder="search" ng-modal="searchName" name="q"> {{searchName}}
  <div class="input-group-btn">
    <a ui-sref="search({search:searchName})">
      <button class="btn btn-default" type="submit"><i class="fa fa-search"></i></button>
    </a>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

尝试一下,让我知道它是否有效

.state('search', {
  name: 'search',
  url: "/search/:search",
  templateUrl: 'search-page',
  controller: function ($stateParams) {
            // If we got here from a url of /search/test54test
    expect($stateParams).toBe({search: "test54test"});
}

您认为

<a ui-sref="search({search: searchName})">View Contact</a>

您可以通过单击Here

找到更多信息