5 lines
28 KiB
JSON

{
"id": "api/common/AsyncPipe",
"title": "AsyncPipe",
"contents": "\n\n<article>\n <div class=\"breadcrumb-container\">\n <div class=\"breadcrumb\">\n <script type=\"application/ld+json\">\n {\n \"@context\": \"http://schema.org\",\n \"@type\": \"BreadcrumbList\",\n \"itemListElement\": [\n { \"@type\": \"ListItem\", \"position\": 1, \"item\": { \"@id\": \"https://angular.io//api\", \"name\": \"API\" } },\n { \"@type\": \"ListItem\", \"position\": 2, \"item\": { \"@id\": \"https://angular.io/api/common\", \"name\": \"@angular/common\" } },\n { \"@type\": \"ListItem\", \"position\": 3, \"item\": { \"@id\": \"https://angular.io/api/common/AsyncPipe\", \"name\": \"AsyncPipe\" } }\n ]\n }\n </script>\n <a href=\"/api\">API</a> > <a href=\"api/common\">@angular/common</a>\n </div>\n <div class=\"github-links\">\n <a href=\"https://github.com/angular/angular/edit/master/packages/common/src/pipes/async_pipe.ts?message=docs(common)%3A%20describe%20your%20change...#L54-L149\" aria-label=\"Suggest Edits\" title=\"Suggest Edits\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">mode_edit</i></a>\n <a href=\"https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/src/pipes/async_pipe.ts#L54-L149\" aria-label=\"View Source\" title=\"View Source\"><i class=\"material-icons\" aria-hidden=\"true\" role=\"img\">code</i></a>\n</div>\n </div>\n \n <header class=\"api-header\">\n <h1 id=\"asyncpipe\">AsyncPipe<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#asyncpipe\"><i class=\"material-icons\">link</i></a></h1>\n \n <label class=\"api-type-label pipe\">pipe</label>\n \n \n <label class=\"api-status-label impure-pipe\">impure</label>\n </header>\n \n <aio-toc class=\"embedded\"></aio-toc>\n\n <div class=\"api-body\">\n \n <section class=\"short-description\">\n <p>Unwraps a value from an asynchronous primitive.</p>\n\n <p><a href=\"api/common/AsyncPipe#description\">See more...</a></p>\n </section>\n \n \n \n<section class=\"pipe-overview\">\n <code-example hidecopy=\"true\" class=\"no-box api-heading\">{{ obj_expression | <span class=\"kwd nocode\">async</span> }}</code-example>\n\n <h2 id=\"ngmodule\">NgModule<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#ngmodule\"><i class=\"material-icons\">link</i></a></h2>\n<ul class=\"ngmodule-list\">\n\n <li>\n <a href=\"api/common/CommonModule\">\n <code-example language=\"ts\" hidecopy=\"true\" class=\"no-box\">CommonModule</code-example>\n </a>\n </li>\n \n</ul>\n\n\n\n \n <h2 id=\"input-value\">Input value<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#input-value\"><i class=\"material-icons\">link</i></a></h2>\n <table class=\"is-full-width list-table parameters-table pipe-parameters\">\n <tbody>\n \n <tr class=\"pipe-parameter\">\n <td class=\"param-name\">\n <a id=\"\"></a>\n <code>obj</code>\n </td>\n <td class=\"param-type\"><code>Subscribable<t> | Promise<t></t></t></code></td>\n <td class=\"param-description\">\n \n \n </td>\n </tr>\n </tbody>\n</table>\n \n \n</section>\n\n\n \n\n \n\n<section class=\"description\">\n <h2 id=\"description\">Description<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#description\"><i class=\"material-icons\">link</i></a></h2>\n <p>The <code><a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a></code> pipe subscribes to an <code>Observable</code> or <code>Promise</code> and returns the latest value it has\nemitted. When a new value is emitted, the <code><a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a></code> pipe marks the component to be checked for\nchanges. When the component gets destroyed, the <code><a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a></code> pipe unsubscribes automatically to avoid\npotential memory leaks.</p>\n\n <p>Further information available in the <a href=\"api/common/AsyncPipe#usage-notes\">Usage Notes</a>...</p>\n</section>\n\n\n \n<section class=\"usage-notes\">\n <h2 id=\"usage-notes\">Usage notes<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#usage-notes\"><i class=\"material-icons\">link</i></a></h2>\n <h3 id=\"examples\">Examples<a title=\"Link to this heading\" class=\"header-link\" aria-hidden=\"true\" href=\"api/common/AsyncPipe#examples\"><i class=\"material-icons\">link</i></a></h3>\n<p>This example binds a <code>Promise</code> to the view. Clicking the <code><a href=\"api/router/Resolve\" class=\"code-anchor\">Resolve</a></code> button resolves the\npromise.</p>\n<code-example path=\"common/pipes/ts/async_pipe.ts\" region=\"AsyncPipePromise\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'async-promise-pipe',\n template: `&#x3C;div>\n &#x3C;code>promise|<a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a>&#x3C;/code>:\n &#x3C;button (click)=\"clicked()\">{{ arrived ? 'Reset' : '<a href=\"api/router/Resolve\" class=\"code-anchor\">Resolve</a>' }}&#x3C;/button>\n &#x3C;span>Wait for it... {{ greeting | <a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a> }}&#x3C;/span>\n &#x3C;/div>`\n})\nexport class AsyncPromisePipeComponent {\n greeting: Promise&#x3C;string>|null = null;\n arrived: boolean = false;\n\n private resolve: Function|null = null;\n\n constructor() {\n this.reset();\n }\n\n reset() {\n this.arrived = false;\n this.greeting = new Promise&#x3C;string>((resolve, reject) => {\n this.resolve = resolve;\n });\n }\n\n clicked() {\n if (this.arrived) {\n this.reset();\n } else {\n this.resolve!('hi there!');\n this.arrived = true;\n }\n }\n}\n\n</code-example>\n<p>It's also possible to use <code><a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a></code> with Observables. The example below binds the <code>time</code> Observable\nto the view. The Observable continuously updates the view with the current time.</p>\n<code-example path=\"common/pipes/ts/async_pipe.ts\" region=\"AsyncPipeObservable\">\n@<a href=\"api/core/Component\" class=\"code-anchor\">Component</a>({\n selector: 'async-observable-pipe',\n template: '&#x3C;div>&#x3C;code>observable|<a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a>&#x3C;/code>: <a href=\"api/common/Time\" class=\"code-anchor\">Time</a>: {{ time | <a href=\"api/common/AsyncPipe\" class=\"code-anchor\">async</a> }}&#x3C;/div>'\n})\nexport class AsyncObservablePipeComponent {\n time = new Observable&#x3C;string>((observer: Observer&#x3C;string>) => {\n setInterval(() => observer.next(new Date().toString()), 1000);\n });\n}\n\n</code-example>\n\n</section>\n\n\n\n </div>\n</article>\n\n<!-- links to this doc:\n - api/common\n - api/common/CommonModule\n - api/common/NgForOf\n - api/common/NgIf\n - api/core/OnDestroy\n - api/core/PipeTransform\n - api/core/testing/async\n - guide/deprecations\n - guide/http\n - guide/migration-update-module-and-target-compiler-options\n - guide/observables-in-angular\n - guide/pipes\n - guide/reactive-forms\n - guide/router-tutorial-toh\n - guide/styleguide\n - guide/template-typecheck\n - guide/testing-components-scenarios\n - guide/updating-to-version-11\n - start/start-data\n - tutorial/toh-pt6\n-->\n<!-- links from this doc:\n - /api\n - api/common\n - api/common/AsyncPipe#asyncpipe\n - api/common/AsyncPipe#description\n - api/common/AsyncPipe#examples\n - api/common/AsyncPipe#input-value\n - api/common/AsyncPipe#ngmodule\n - api/common/AsyncPipe#usage-notes\n - api/common/CommonModule\n - api/common/Time\n - api/core/Component\n - api/router/Resolve\n - https://github.com/angular/angular/edit/master/packages/common/src/pipes/async_pipe.ts?message=docs(common)%3A%20describe%20your%20change...#L54-L149\n - https://github.com/angular/angular/tree/12.0.0-next.7/packages/common/src/pipes/async_pipe.ts#L54-L149\n-->"
}