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:
- 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:
The router is configured as follows:
$stateProvider
.state('orderdetail', {
url: '/orders/:id',
template: '<my-order-detail></my-order-detail>'
});
}
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;
}
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>'
Is this possible?