2017-06-30 96 views
2

我有一個子組件,它通過接收@Input()一些值:Angular2頻率值

<my-component 
    [model]="node" 
    [required]="isRequired()" 
> 
</my-component> 

[required]經過isRequired()方法的返回值,你可以看到。 但它可能會變得相當飢餓,所以我想限制它被查詢的頻率。

有什麼方法可以配置角度調用isRequired()多久?

+1

你還好嗎使用'Observable'或者你需要一些舊代碼(角1)遷移至4角,不能夠以'Observable'在過程中過早使用? – andreim

+0

這裏的一些信息可能會有所幫助:https://blog.thoughtram.io/angular/2016/02/22/angular-2-change-detection-explained.html – DeborahK

+0

@andreim我不會遷移。 「可觀察」會沒事的,我想。但我不知道這是領先的。 – Reynicke

回答

0

不,沒有辦法做到這一點。 Angular爲父組件創建了一個工廠,該工廠包含MyComponent以及在檢查父組件時在每個更改檢測週期調用的updateDirectives函數。詳情請參閱The mechanics of property bindings update in Angular。您無法禁用專門用於綁定的更改檢測。如果分離變化檢測器,則不會執行operations from change detection

您可能會做的是在父組件上創建提供程序服務,並讓您的子組件組件將其注入並在需要時查詢isRequired。沿着這些路線的東西:

@Component({template: `<my-component...>`, provider: [SomeService]}) 
export class ParentComponent {} 

@Component({}) 
export class MyComponent { 
    constructor(service: SomeService) { 
    service.isRequired(); 
    } 
}