angularjs ui-sref address named view

377 views Asked by At

I would to make something like this:

.state('tabs.order', {
    url: "/order/:orderId",
    views: {
        'orders-tab': {
            templateUrl: "templates/orderDetail.html",
            controller: 'OrderDetailController'
        },
        'orders-all-tab': {
            templateUrl: "templates/orderDetail.html",
            controller: 'OrderDetailController'
        }
    }
})

then in my view i would to put a conditional ui-sref in which i can choose the tab to be addressed; something like this:

ui-sref="tabs.order({orderId:order.ID}, <orders-tab or orders-all-tab?>)"

would this be possible? Thanks

1

There are 1 answers

0
Razvan B. On

Well, I don't think there is any way yet to specify the view in ui-sref, but here is a starting point which I hope it will help you..

Firstly, the routes:

app.config(function($stateProvider, $urlRouterProvider){

$urlRouterProvider.otherwise("/order/all-orders");

$stateProvider
    .state("order", { abtract: true, url:"/order", templateUrl:"main.html" })
        .state("order.orderTab", { url: "/order-details", templateUrl: "orderDetails.html" })
        .state("order.orderAllTab", { url: "/all-orders", templateUrl: "allOrders.html" });
});


Then, define your main page (Orders and Order Details) [main.html]

<div ng-controller="mainController">
    <tabset>
        <tab 
            ng-repeat="t in tabs" 
            heading="{{t.heading}}"
            select="go(t.route)"
            active="t.active">
        </tab>
    </tabset>
    <h2>View:</h2>
    <div ui-view></div>
</div>


And the page controller to specify the tab data:

app.controller("mainController", function($rootScope, $scope, $state) {     

    $scope.go = function(route){
        $state.go(route);
    };

    $scope.active = function(route){
        return $state.is(route);
    };

    $scope.tabs = [
        { heading: "Order", route:"order.orderTab", active:true },
        { heading: "All Orders", route:"order.orderAllTab", active:false },
    ];

    $scope.$on("$stateChangeSuccess", function() {
        $scope.tabs.forEach(function(tab) {
            tab.active = $scope.active(tab.route);
        });
    });
});


All you have to do next is to include the orderID.
Here is a demo