This post will teach us how to recognize an event in an Angular application when a user clicks outside of a component.
Required conditions
Basic familiarity with Visual Studio Code, Angular 2 or higher, Node and NPM installed, and Bootstrap
Make an Angular Project
The command to create an Angular project is as follows.
Create an Angular Project
Create an Angular project by using the following command.
1 | ng new angapp |
Now install Bootstrap by using the following command,
1 | npm install bootstrap --save |
Now open the styles.css file and add Bootstrap file reference. To add a reference in the styles.css file add this line.
1 | @import '~bootstrap/dist/css/bootstrap.min.css'; |
1 | ng g c actionmenu |
Now open actionmenu.component.html file and add the following code.
1 2 3 4 5 6 7 8 | <span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>sidebar {{show}} panel-group<span class="token punctuation">"</span></span> <span class="token attr-name">clickOutside</span> <span class="token attr-name">(clickOutside)</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>works()<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span> panel panel-default<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>div<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>panel-body<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Panel Body<span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"><</span>div <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>panel-footer<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Panel Footer<span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span> <span class="token tag"><span class="token punctuation"></</span>div<span class="token punctuation">></span></span> |
Now open actionmenu.component.ts file and add the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">Component</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Input</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span> @<span class="token function"><span class="token maybe-class-name">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">selector</span><span class="token operator">:</span> <span class="token string">'app-actionmenu'</span><span class="token punctuation">,</span> <span class="token literal-property property">templateUrl</span><span class="token operator">:</span> <span class="token string">'./actionmenu.component.html'</span><span class="token punctuation">,</span> <span class="token literal-property property">styleUrls</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./actionmenu.component.css'</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword module keyword-export">export</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">ActionmenuComponent</span> <span class="token punctuation">{</span> @<span class="token function"><span class="token maybe-class-name">Input</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> show<span class="token operator">:</span> boolean<span class="token operator">=</span><span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token function">works</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-this">this</span><span class="token punctuation">.</span><span class="token property-access">show</span> <span class="token operator">=</span> <span class="token operator">!</span><span class="token keyword keyword-this">this</span><span class="token punctuation">.</span><span class="token property-access">show</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Now open actionmenu.component.css file and add the following code.
1 2 3 4 5 6 7 8 9 10 11 | <span class="token punctuation">.</span><span class="token property-access">sidebar</span> <span class="token punctuation">{</span> <span class="token literal-property property">display</span><span class="token operator">:</span> none<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span><span class="token boolean">true</span> <span class="token punctuation">{</span> <span class="token literal-property property">display</span><span class="token operator">:</span> block<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">.</span><span class="token boolean">false</span> <span class="token punctuation">{</span> <span class="token literal-property property">display</span><span class="token operator">:</span> none<span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Now open app.component.html file and add the following code.
1 2 3 4 5 6 7 8 9 | <span class="token operator"><</span>div <span class="token keyword keyword-class">class</span><span class="token operator">=</span><span class="token string">"container"</span> style<span class="token operator">=</span><span class="token string">"margin-top:10px;margin-bottom: 24px;"</span><span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword keyword-class">class</span><span class="token operator">=</span><span class="token string">"col-sm-12 btn btn-info"</span><span class="token operator">></span> <span class="token maybe-class-name">How</span> to detect clicks outside <span class="token keyword keyword-in">in</span> <span class="token maybe-class-name">Angular</span> <span class="token maybe-class-name">Application</span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> <span class="token operator"><</span>div <span class="token keyword keyword-class">class</span><span class="token operator">=</span><span class="token string">"container"</span><span class="token operator">></span> <span class="token operator"><</span>button type<span class="token operator">=</span><span class="token string">"button"</span> <span class="token keyword keyword-class">class</span><span class="token operator">=</span><span class="token string">"btn btn-success"</span> <span class="token punctuation">(</span>click<span class="token punctuation">)</span><span class="token operator">=</span><span class="token string">"show = !show"</span><span class="token operator">></span><span class="token maybe-class-name">Primary</span><span class="token operator"><</span><span class="token operator">/</span>button<span class="token operator">></span> <span class="token operator"><</span>app<span class="token operator">-</span>actionmenu <span class="token punctuation">[</span>show<span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">"show"</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>app<span class="token operator">-</span>actionmenu<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>div<span class="token operator">></span> |
Now open app.component.ts file and add the following code.
1 2 3 4 5 6 7 8 9 10 11 | <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">Component</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span> @<span class="token function"><span class="token maybe-class-name">Component</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">selector</span><span class="token operator">:</span> <span class="token string">'app-root'</span><span class="token punctuation">,</span> <span class="token literal-property property">templateUrl</span><span class="token operator">:</span> <span class="token string">'./app.component.html'</span><span class="token punctuation">,</span> <span class="token literal-property property">styleUrls</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'./app.component.css'</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword module keyword-export">export</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">AppComponent</span> <span class="token punctuation">{</span> title <span class="token operator">=</span> <span class="token string">'Angular App'</span><span class="token punctuation">;</span> <span class="token literal-property property">show</span><span class="token operator">:</span>boolean<span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Now create a new directive by using the following command, and the following code in this directive.
1 | ng g directive clickOutside |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">Directive</span><span class="token punctuation">,</span> <span class="token maybe-class-name">ElementRef</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Input</span><span class="token punctuation">,</span> <span class="token maybe-class-name">Output</span><span class="token punctuation">,</span> <span class="token maybe-class-name">HostListener</span><span class="token punctuation">,</span> <span class="token maybe-class-name">EventEmitter</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span> @<span class="token function"><span class="token maybe-class-name">Directive</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">selector</span><span class="token operator">:</span> <span class="token string">'[clickOutside]'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword module keyword-export">export</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">ClickOutsideDirective</span> <span class="token punctuation">{</span> <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token parameter"><span class="token keyword keyword-private">private</span> <span class="token literal-property property">elementRef</span><span class="token operator">:</span> <span class="token maybe-class-name">ElementRef</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span> @<span class="token function"><span class="token maybe-class-name">Output</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> clickOutside<span class="token operator">:</span> <span class="token maybe-class-name">EventEmitter</span><span class="token operator"><</span>any<span class="token operator">></span> <span class="token operator">=</span> <span class="token keyword keyword-new">new</span> <span class="token class-name">EventEmitter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> @<span class="token function"><span class="token maybe-class-name">HostListener</span></span><span class="token punctuation">(</span><span class="token string">'document: click'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'$event.target'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token function">onMouseEnter</span><span class="token punctuation">(</span><span class="token parameter"><span class="token literal-property property">targetElement</span><span class="token operator">:</span>any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-const">const</span> clickInside <span class="token operator">=</span> <span class="token keyword keyword-this">this</span><span class="token punctuation">.</span><span class="token property-access">elementRef</span><span class="token punctuation">.</span><span class="token property-access">nativeElement</span><span class="token punctuation">.</span><span class="token method function property-access">contains</span><span class="token punctuation">(</span>targetElement<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword control-flow keyword-if">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>clickInside<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword keyword-this">this</span><span class="token punctuation">.</span><span class="token property-access">clickOutside</span><span class="token punctuation">.</span><span class="token method function property-access">emit</span><span class="token punctuation">(</span><span class="token keyword null nil keyword-null">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Now open app.module.ts and following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">NgModule</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/core'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">BrowserModule</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/platform-browser'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">FormsModule</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/forms'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">HttpClientModule</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'@angular/common/http'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">AppRoutingModule</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'./app-routing.module'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">AppComponent</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'./app.component'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ActionmenuComponent</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'./actionmenu/actionmenu.component'</span><span class="token punctuation">;</span> <span class="token keyword module keyword-import">import</span> <span class="token imports"><span class="token punctuation">{</span> <span class="token maybe-class-name">ClickOutsideDirective</span> <span class="token punctuation">}</span></span> <span class="token keyword module keyword-from">from</span> <span class="token string">'./click-outside.directive'</span><span class="token punctuation">;</span> @<span class="token function"><span class="token maybe-class-name">NgModule</span></span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">declarations</span><span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token maybe-class-name">AppComponent</span><span class="token punctuation">,</span> <span class="token maybe-class-name">ActionmenuComponent</span><span class="token punctuation">,</span><span class="token maybe-class-name">ClickOutsideDirective</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token literal-property property">imports</span><span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token maybe-class-name">BrowserModule</span><span class="token punctuation">,</span> <span class="token maybe-class-name">AppRoutingModule</span><span class="token punctuation">,</span> <span class="token maybe-class-name">FormsModule</span><span class="token punctuation">,</span> <span class="token maybe-class-name">HttpClientModule</span> <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token literal-property property">providers</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token literal-property property">bootstrap</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token maybe-class-name">AppComponent</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword module keyword-export">export</span> <span class="token keyword keyword-class">class</span> <span class="token class-name">AppModule</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> |
Now run the application using npm start and check the result.
This article teaches us how to build a custom file size pipe that transforms a file’s bytes into a format that can be read by humans.
Best and Cheap AngularJS 1.7.2 Hosting Australia
Are you looking for best and cheap AngularJS 1.7.2 hosting Australia? While choosing the right AngularJS 1.7.2 web hosting service, the hosting server must be Windows with multiple .net frameworks as well as SQL Server configured. You must ensure that your hosting plan provides sql server remote access. Since it requires extra configuration on IIS server in order to generate your reports, AngularJS 1.7.2 web hosting is more expensive than asp.net hosting. Most of the shared Windows hosting companies do not provide AngularJS 1.7.2 because of license fees.
Due to the resources AngularJS 1.7.2 uses, there’re only a few web hosting companies can support AngularJS 1.7.2 web service. For fast, secure and reliable AngularJS 1.7.2 Web Hosting Australia, DiscountService.biz web solutions will be one of your best choice.