代码之家  ›  专栏  ›  技术社区  ›  KingFish

将参数传递回组件[重复]

  •  0
  • KingFish  · 技术社区  · 8 年前

    我遇到以下代码最糟糕的时候:

    angular.
        module('albumCarousel').
        component('albumCarousel', {
            templateUrl: '/album-carousel/album-carousel.template.html',
    
            controller: function AlbumCarouselController($scope, $http) {
                /* the bane of my existence */
                console.log(this.category); // prints undefined..
            }
        }
    )
    

    在我的HTML中,我有以下内容:

    <angular-carousel category="songs"></angular-carousel>
    

    回到控制器中的组件,

    this.category comes back as undefined.
    

    我怎样才能让它返回“歌曲”

    1 回复  |  直到 8 年前
        1
  •  0
  •   KingFish    8 年前

    经过一番谷歌搜索,我找到了答案:$OnInit:

    this.$onInit=function() {
        console.log(this.category)
    }
    

    我还必须添加一个绑定:

        bindings: {
            category: '@',
        }
    

    谢谢你的帮助