Angular 1.6 bindings inside controller
Asked Answered
C

2

6

Im trying to pass some parameters to my component through bindings, but unfortunately I'm not having luck in using those params in my controller, this is my code:

angular.module('project1').component('menu', {
    templateUrl: '/static/js/templates/menu.template.html',
    bindings: {
        rid: '@'
    },
    controller: ['Restaurant', function RestaurantListController(Restaurant) {
        console.log(this.rid);
        console.log(this);
        this.restaurant = Restaurant.get({restaurantId: this.rid});
    }]
});

HTML component:

<menu rid="1"></menu>

The interesting thing is that i can access the parameters in the template and when i do the 2 console log, the first one is undefined, but in the second one i can see the rid variable...so, i really don't understand what i'm missing.

Corpuz answered 25/12, 2016 at 17:8 Comment(1)
add html component useBristol
I
9

With angular 1.6, your bindings are going to be ready on the method $onInit and not before.

If you need to re-enable auto bindings https://toddmotto.com/angular-1-6-is-here#re-enabling-auto-bindings

Incus answered 25/12, 2016 at 17:29 Comment(1)
Thank you so much!Corpuz
S
0

If anyone still searching for solution, use $onInit method provided by angular.

            this.$onInit = function () {
            $http.get(`/api/v1/projects`).then((res) => {
                $scope.projects = res.data;
            }, (err) => {
                $scope.error = err
            })
        };
Strew answered 26/3, 2020 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.