Angular 1.5中的绑定对象

时间:2017-03-05 21:54:41

标签: angularjs data-binding components 2-way-object-databinding

我有以下代码:

class TreeComponent {

    entity: any;

    constructor(private $rootScope: ng.IScope) {
        console.log(this.entity);         
    }
}


AppModule.component('tree', {

    templateUrl: '<tree entity="obj"></tree>',
    controller: ['$rootScope', TreeComponent],
    bindings: {
        entity: '='
    }

})

我希望console.log(this.entity)打印插入'tree'标签的obj。我不知道为什么,但它打印'undefined'。你知道为什么吗?

1 个答案:

答案 0 :(得分:0)

最后,我发现'entity'属性仅在离开构造函数后绑定到对象。例如,

class TreeComponent {

    entity: any;

    constructor(private $rootScope: ng.IScope) {
        console.log(this.entity);         
    }
    debug() {
        console.log(this.entity);// it works
    }

}