How to use @HostBinding with @Input properties in Angular 2?

前端 未结 5 1664
青春惊慌失措
青春惊慌失措 2020-12-29 20:27

(Angular 2 RC4)

With @HostBinding we should be able to modify properties of the host, right? My question is, does this apply to @Input() properties as well and if so

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 20:42

    BEWARE on using get with hostbinding.

    At least I an endless recursion (thousands of cycles). Put a console.log in the get function to check:

    @HostBinding('@someAnim') get whatever() {
        console.log('#### traffic.....');
        return true;
    }
    

    I even got that trouble, when just changing some class (so nothing around fancy Angular animation) and not even relying on any other class member whatsoever:

    @HostBinding('class.foobar') get _____() {
        console.log('#### more traffic.....');
        return true;
    }
    

    An addition, it doesn't hurt to have this in your component while developing (as a “free flow canary”):

    ngOnChanges(changes: SimpleChanges) {
        console.log('#### more traffic.....');
    }
    

    You should only see one log entry per, say, state-change and instantiated component, anyway not thousands...

    —At last— I had luck with this one:

    • distinct variable for incoming (@Input) and outgoing (@HostBinding)
    • changing the outgoing upon change
    • → no „free flow“
        @Input() animTileAttrib = false;
        @HostBinding('@animTile') paramPack: AnimParams = { value: false, params: {}};
    
        ngOnChanges(changes: SimpleChanges) {
            console.log('#### traffic.....');
            if ('animTileAttrib' in changes) {
                this.paramPack = { value: changes.animTileAttrib.currentValue, params: {} };
            }
        }
    

提交回复
热议问题