0

My angular ui router is changing states/views as:

    $stateProvider
        .state({
            name: 'home',
            url: '/',
            template: '<home-view></home-view>',
        })

Is there way pass bound parameters to < home-view > in UI router ?

E.g.

<home-view my-data="dataObject">

or

<home-view my-data="myService.getMyData()">

Controller for my-view is located in own file with the actual template & directive.

2
  • Why not pass it via the controller of the view? That's what it's for :) Commented Nov 27, 2016 at 20:02
  • Well I managed to get it working with controller function and $scope.$parent.myData. However is it really so that it cannot be passed via template like with directive & data binding ? Commented Nov 27, 2016 at 20:30

2 Answers 2

5

In the end colleague of mine helped with this issue. Solution uses resolve to get the data and then passes it as router scope variable to the template. Important learning was that controller defined in the router has it's own scope and not scope of the "home-view". In my home controller router controller could be accessed via $scope.parent.

.state({
    name: 'home',
    url: '/',
    resolve: {
        cData: (MyService) => {
            return MyService.getMyData()
        }
    },
    controller: function (cData) {
        this.data = cData;
    },
    controllerAs: 'ctrl',
    template: '<home-view my-data="ctrl.data"></home-view>'
})
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a resolver

$stateProvider
    .state({
        name: 'home',
        controller : "MyController"
        url: '/',
        resolve: {
            obj: function() {
                return {prop:"hello!"}
            }
        }
        template: '<home-view></home-view>',
})

// MyController
angular.module('app').controller('MyController', function(obj) {
    console.log(obj.prop); // hello!
});

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.