AngularJS : Making UI-Router work without a URL

廉价感情. 提交于 2019-12-11 13:05:38

问题


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:

  1. 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.
  2. 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?


回答1:


In case, I do understand your goal correctly, we can have this template, passing the id into directive (and its isolated scope).

There is a working plunker

$stateProvider
    .state('orderdetail', {
      url: '/orders/:id',
      //template: '<my-order-detail></my-order-detail>',
      template: '<my-order-detail data-id="$stateParams.id"></my-order-detail>',
    });

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;
    }
])

And this is the way its called

<a ui-sref="orderdetail({id:1})">
<a ui-sref="orderdetail({id:2})">

Check that in action here



来源:https://stackoverflow.com/questions/29423241/angularjs-making-ui-router-work-without-a-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!