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

javascript - Angular 2, Does @Input with a setter behave differently than @Input without a setter? - Stack Overflow

programmeradmin1浏览0评论

I am wondering if someone could elaborate on this. Does @Input() with a setter vs using @Input() without a setter behave differently in regards to change detection?

For example:

@Input() something: SomeType; 

-vs-

private _something;

@Input() set something(something: SomeType ) {
  this._something = something;
}

get something(): SomeType {
  return this._something;
}

The obvious difference is that the setter/getter allows @Input() with some extra logic. But does this affect change detection in a different way than if I were to use @Input() without a setter?

I am wondering if someone could elaborate on this. Does @Input() with a setter vs using @Input() without a setter behave differently in regards to change detection?

For example:

@Input() something: SomeType; 

-vs-

private _something;

@Input() set something(something: SomeType ) {
  this._something = something;
}

get something(): SomeType {
  return this._something;
}

The obvious difference is that the setter/getter allows @Input() with some extra logic. But does this affect change detection in a different way than if I were to use @Input() without a setter?

Share Improve this question asked Jan 31, 2017 at 17:08 khollenbeckkhollenbeck 16.2k18 gold badges68 silver badges102 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

For angular not a lot changes. The input won't be set or the setter won't be called more often. There is however a caveat where there is more logic inside the setter which can trigger another change detection. If you have that, angular will throw the known error (only in development mode)

Expression has changed after it was checked.

So, the change detector does not behave differently, but issues may arise depending on what extra logic you put inside the setter

From angular's view, the only difference is that you got a chance to hook those @Input()'s get/set functions.

From Javascripts view, first will be "just" a property, second would use Object.defineProperty.

plunker: https://plnkr.co/edit/1koamZCvyG5YAIPNB73r?p=preview

piled code with setter:

Object.defineProperty(AppComponent.prototype, "test1", {
    get: function () { return this._test1; },
    set: function (val) {
        console.log('test1 was set!');
        this._test1 = val;
    },
    enumerable: true,
    configurable: true
});
__decorate([
    __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["Input"])(), 
    __metadata('design:type', Object), 
    __metadata('design:paramtypes', [Object]) /* difference? */
], AppComponent.prototype, "test1", null);

piled code withOUT setter:

__decorate([
    __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["Input"])(), 
    __metadata('design:type', Object)
], AppComponent.prototype, "test2", void 0);

both piled with angular-cli: ng build.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论