最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 1.6 bindings inside controller - Stack Overflow

programmeradmin0浏览0评论

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

angular.module('project1')ponent('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 ponent:

<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.

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

angular.module('project1').ponent('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 ponent:

<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.

Share Improve this question edited Jan 25, 2017 at 12:30 Mistalis 18.3k14 gold badges77 silver badges97 bronze badges asked Dec 25, 2016 at 17:08 Dave PlugDave Plug 1,0791 gold badge11 silver badges23 bronze badges 1
  • add html ponent use – RIYAJ KHAN Commented Dec 25, 2016 at 17:20
Add a ment  | 

2 Answers 2

Reset to default 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./angular-1-6-is-here#re-enabling-auto-bindings

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
            })
        };
发布评论

评论列表(0)

  1. 暂无评论