AngularJS:让 UI-Router 在没有 URL 的情况下工作 [英] AngularJS : Making UI-Router work without a URL
问题描述
我正在使用 Angular UI 路由器创建一个主从页面.我已经能够使它工作 - 当我单击主记录时,此记录的 ID 会附加到 URL 并且详细视图显示此记录的详细信息.但是我有两个问题:
I am creating a master-detail page using the Angular UI router. I have been able to make it work - when I click on a master record, the id of this record is appended to the URL and the detail view shows the detail for this record. However I have two questions:
- 是否可以在不更改 URL 的情况下完成这项工作?据我了解,UI 路由器是关于状态更改的,不一定是 url 更改.但是,如果我从路由器状态中删除 url 属性,则不会显示详细信息.
- 与我的详细信息状态关联的模板是一个指令.目前该指令使用
$stateParams
获取主记录的 id.这使得它依赖于 UI 路由器.是否可以使用隔离范围传递此 ID?
- Is it possible to make this work without a change in the URL? As I understand it the UI router is all about state changes, not necessarily url changes. However, if I remove the url property from the router state, then detail doesn't show up.
- The template associated with my detail state is a directive. Currently this directive gets the id of the master record using
$stateParams
. This makes it dependent on the UI router. Is it possible to pass this id down using isolate scope?
这是我的代码的关键部分:
Here's are the key pieces of my code:
路由器配置如下:
$stateProvider
.state('orderdetail', {
url: '/orders/:id',
template: '<my-order-detail></my-order-detail>'
});
}
正如上面#1 中提到的,如果我注释掉 url,那么详细信息将停止出现.为什么?有没有办法在不更改 URL 的情况下完成这项工作?
As mentioned in #1 above, if I comment out the url, then detail stops appearing. Why? Is there a way to make this work without having the URL to change?
选择主记录后,我将状态更改如下:
When the master record is selected I change the state as follows:
$state.go('orderdetail', {
id: vm.selectedOrderId
});
这是显示详细信息的指令的代码:
Here's the code for the directive that shows the detail:
angular.module('app.orderdetail', []);
angular.module('app.orderdetail')
.directive('myOrderDetail', orderDetailDirective)
.controller('OrderDetailController', OrderDetailController);
// ----- orderDetailDirective -----
orderDetailDirective.$inject = [];
function orderDetailDirective() {
var directive = {
restrict: 'E',
templateUrl: 'components/orderdetail/orderdetail.html',
scope: {
id: '='
},
controller: 'OrderDetailController',
controllerAs: 'vm'
};
return directive;
}
// ----- OrderDetailController -----
OrderDetailController.$inject = ['$stateParams'];
/* @ngInject */
function OrderDetailController($stateParams) {
var vm = this;
vm.id = $stateParams.id;
}
请注意,控制器使用 $stateParams
捕获所选主记录的 id.我很想通过切换到隔离范围来删除指令中的这种依赖关系.因此,与 UI 路由器状态关联的模板应如下所示:
Note that the controller captures the id of the selected master record using $stateParams
. I would love to remove this dependency in the directive by switching to isolate scope. So the template associated with the UI router state should look something like this:
template: '<my-order-detail data-id={{vm.id}}></my-order-detail>'
这可能吗?
推荐答案
如果我确实理解您的目标,我们可以使用这个 template
,通过 id
到指令 (及其隔离范围).
In case, I do understand your goal correctly, we can have this template
, passing the id
into directive (and its isolated scope).
$stateProvider
.state('orderdetail', {
url: '/orders/:id',
//template: '<my-order-detail></my-order-detail>',
template: '<my-order-detail data-id="$stateParams.id"></my-order-detail>',
});
刚刚将其添加到我们的代码中(所以我们不需要控制器,$stateParams
已经在每个视图状态模板的范围内):
Just added this to our code (so we do not need controller, $stateParams
are already in scope of each view state template):
.run(['$rootScope', '$state', '$stateParams',
function($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
}
])
这就是它的名字
<a ui-sref="orderdetail({id:1})">
<a ui-sref="orderdetail({id:2})">
在此处操作
这篇关于AngularJS:让 UI-Router 在没有 URL 的情况下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!