Providing Context Variables to Directives
Suppose we want to record some metadata on how our directive affected components and make this data available to them. For example, in our appDelay
directive, we're making a setTimeout
call, which in JavaScript's single-threaded asynchronous model means that it may not run after the exact time we provided. We'll capture the exact time it loads and make that variable available in the template.
We've made a few changes to our appDelay
directive. We've created a new DelayContext
class that contains the context that we want to provide to our directive. In this case, we want to capture the actual time the createEmbeddedView
call occurs and make that available as loadTime
in our directive. We've also provided our new class as the generic argument to the TemplateRef
function. This enables static analysis and lets us make sure our calls to createEmbeddedView
pass in a variable of type DelayContext
. In our createEmbeddedView
call we pass in our variable which has captured the time of the method call.
In the component using appDelay
, we access the loadTime
context variable in the same way we access variables in ngFor
.
Last updated