0

I am working with angular and using ui router. The structure of my views are something like this:

<div ui-view>
    <div ui-view="left">
        -- list of items
    </div>
    <div ui-view="right">
        -- details of item
    </div>
</div>

Now if i select an item from left panel, an api is called to get the details of that item. Now want to load that information in right view.

All the view states have their own controller. Can someone please help me to pass the returned data from api tosecond view?

1
  • you can use $service to get the data from database and then inject it to other controller where you want to render. We can communicate between controller via $service, $factory etc. in angular. Commented Aug 30, 2016 at 0:24

1 Answer 1

1

This is a pretty straightforward and common use case with ui-router. The main concept is of nested states and multiple named-views.

I would set this up with some states that look something like:

$stateProvider
    .state('user', {
        url: '/user',
        views: {
            'left': {
                templateUrl: '/templates/states/user-list.tpl.html'
            },
            'right': {
                template: '<p>This is default content that would appear in the right view until a user is selected.  This is nice, but is completely optional.</p>'
            }
        }
    })
    .state('user.details', {
        url: '/:userId',
        views: {
            'right': {
                templateUrl: '/templates/states/user-details.tpl.html'
            }
        }
    });

In the user-list template, you would use links something like:

<a ui-sref="user.details({userId: user.id})">{{user.name}}</a>

Because user.details is a child state of user, the user-list stays loaded into the left view while navigating through different users in the right view.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.