Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 我是否可以使用UI路由器将变量传递到控制器&;ui sref?_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs 我是否可以使用UI路由器将变量传递到控制器&;ui sref?

Angularjs 我是否可以使用UI路由器将变量传递到控制器&;ui sref?,angularjs,angular-ui-router,Angularjs,Angular Ui Router,我想知道是否有可能通过UI路由器传递多个变量,以便在一个州的控制器中使用 在HTML中,我有 <li class="blist-item grid-loader" ng-repeat="item in items"> <a ui-sref="item({ id: {{item.$id}} })"><h3>{{item.title}}</h3></a> </li> 现在,上面的配置起作用了,但问题是每个项目的唯一ID

我想知道是否有可能通过UI路由器传递多个变量,以便在一个州的控制器中使用

在HTML中,我有

<li class="blist-item grid-loader" ng-repeat="item in items">
    <a ui-sref="item({ id: {{item.$id}} })"><h3>{{item.title}}</h3></a>
</li>
现在,上面的配置起作用了,但问题是每个项目的唯一ID是一个不可理解的字符串,它给我留下了诸如“/item/-sds43sd23s”之类的链接

我想弄明白的是:

(1)如何将URL定向到项目标题,

(2)同时使用项目ID作为获取项目对象数据的主要方法。



是否有一种方法可以将两个变量(项目的ID和项目的标题)传递到路由器?

我想说,您的问题有一个解决方案。此问答应能解决此问题:。这有一个链接

我们需要这个状态定义(与上面的代码片段完全相同),它无论如何都不会限制id(例如,{id:[0-9a-fA-F]{1,8}}这将避免传递标题…)

下一步是要有一个
服务
,它能够通过标题获取ID

.factory('ItemSvc', ...
  // some service, converting title into ID
  ...
  var getIdByTitle = function(title){
     // some how get the ID for a Title
     ...
  }
最后,我们将使用
UI路由器
特性


检查

您的所有标题是否也将是唯一的?是的,每个标题将与每个ID唯一关联。嘿@Radim,这条评论早该发表了,但经过一点修改,您的代码肯定会让我找到正确的答案。非常感谢你花时间回答这个问题!很高兴见到你,先生!享受神奇的工具
UI路由器
;)
.state('item', {
    url: '/item/:id', // no restrcitions, this is what we need
    ...
.factory('ItemSvc', ...
  // some service, converting title into ID
  ...
  var getIdByTitle = function(title){
     // some how get the ID for a Title
     ...
  }
$urlRouterProvider.when(/item\/[a-zA-Z\-]+/,
  function($match, $state, ItemSvc) {

    // get the Title 
    var title = $match.input.split('item/')[1];
    // get some promise resolving that title
    // converting it into ID
    var promiseId = ItemSvc.getIdByTitle(title);

    promiseId.then(function(id){

      // once ID is recieved... we can go to the detail
      $state.go('item', {id: id}, {location: false}); 
    })

    // essential part! this will instruct UI-Router, 
    // that we did it... no need to resolve state anymore
    return true;
  }
);