﻿
{"id":7633,"date":"2018-04-14T13:41:40","date_gmt":"2018-04-14T13:41:40","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=7633"},"modified":"2018-04-14T13:41:40","modified_gmt":"2018-04-14T13:41:40","slug":"angular-4-ngif-direktifi","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/angular-4-ngif-direktifi\/","title":{"rendered":"Angular 4 &#8211; ngIf Direktifi"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde; Angular uygulamalar\u0131nda, yap\u0131sal olarak bildi\u011finiz klasik &#8220;if &#8211; else if &#8211; else&#8221; mant\u0131\u011f\u0131nda DOM nesnelerine belirli \u015fartlara g\u00f6re kontrol sa\u011flamaya ve m\u00fcdahalede bulunmaya yarayan ngIf direktifi \u00fczerine konu\u015fuyor olaca\u011f\u0131z.<\/p>\n<h4><span style=\"color: #800080;\">ngIf ile If Yap\u0131s\u0131<\/span><\/h4>\n<p>\u0130lk olarak ngIf direktifinin en sade prototipini ele alarak konuya ba\u015flayal\u0131m;<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;&#x5B;HTML NESNES\u0130] *ngIf=&quot;&#x5B;\u015eART]&quot;&gt;\r\n<\/pre>\n<p>Yukar\u0131daki prototipi incelerseniz e\u011fer herhangi bir html nesnesine verilen<span style=\"font-size: 12px;\">(div, span, hr, br vs. vs.)<\/span> ngIf direktifi i\u00e7erisindeki \u015fart ge\u00e7erliyse e\u011fer ilgili html nesnesi sayfaya bas\u0131lacakt\u0131r.<\/p>\n<p>\u015eimdi a\u015fa\u011f\u0131daki \u00f6rnekleri inceleyiniz.<\/p>\n<ul>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;3 == 3&quot;&gt;\r\n  Merhaba\r\n&lt;\/div&gt;\r\n<\/pre>\n<p><a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-ngIf-Direktifi.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7637\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-ngIf-Direktifi.png\" alt=\"Angular 4 - ngIf Direktifi\" width=\"65\" height=\"28\" \/><\/a><\/li>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;false&quot;&gt;\r\n  Merhaba\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>\u015eart &#8220;false&#8221; oldu\u011fu i\u00e7in div nesnesini ekrana basmayacakt\u0131r.<\/li>\n<\/ul>\n<p>E\u011fer ki component class i\u00e7erisinde tan\u0131mlanan de\u011fi\u015fkenler \u00fczerinde ngIf yap\u0131s\u0131n\u0131 kullanmak istiyorsan\u0131z a\u015fa\u011f\u0131daki \u00f6rneklerde oldu\u011fu gibi \u00e7al\u0131\u015fabilirsiniz.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-ng-if-example',\r\n  templateUrl: '.\/ng-if-example.component.html',\r\n  styleUrls: &#x5B;'.\/ng-if-example.component.css']\r\n})\r\nexport class NgIfExampleComponent implements OnInit {\r\n  deger1: number = 100;\r\n  deger2: string = &quot;Merhaba&quot;;\r\n  deger3: boolean = false;\r\n  constructor() { }\r\n  ngOnInit() {\r\n  }\r\n}\r\n<\/pre>\n<ul>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;deger1&quot;&gt;\r\n  {{deger1}}\r\n&lt;\/div&gt;\r\n&lt;div *ngIf=&quot;deger2&quot;&gt;\r\n  {{deger2}}\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Bu \u015fekilde ilgili de\u011fi\u015fkenin de\u011feri var m\u0131? yok mu? kontrol ediliyor. E\u011fer varsa ilgili html nesnesi ekrana bas\u0131l\u0131yor.<br \/>\n<a href=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-ngIf-Direktifi-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7642\" src=\"http:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2018\/04\/Angular-4-ngIf-Direktifi-1.png\" alt=\"Angular 4 - ngIf Direktifi\" width=\"74\" height=\"49\" \/><\/a><\/li>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;!deger3&quot;&gt;\r\n  {{deger3}}\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>&#8220;True&#8221; de\u011feri yaz\u0131lacakt\u0131r.<\/li>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;deger1 &gt;= 100&quot;&gt;\r\n  {{deger1}}\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>&#8220;100&#8221; de\u011feri yaz\u0131lacakt\u0131r.<\/li>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;deger1 &gt;= 100 &amp;&amp; deger2 == 'Merhaba'&quot;&gt;\r\n {{deger2}} {{deger1}}\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>&#8220;Merhaba 100&#8221; de\u011feri yaz\u0131lacakt\u0131r.<\/li>\n<\/ul>\n<h4><span style=\"color: #800080;\">ngIf ile If &#8211; Else Yap\u0131s\u0131<\/span><\/h4>\n<p>E\u011fer ki ngIf direktifi ile &#8220;if &#8211; else if&#8221; \u015feklinde \u015fart ko\u015fmak istiyorsan\u0131z a\u015fa\u011f\u0131daki prototipte oldu\u011fu gibi \u00e7al\u0131\u015fman\u0131z gerekmektedir.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&lt;&#x5B;HTML NESNES\u0130] *ngIf=&quot;&#x5B;\u015eART]; else &#x5B;GOSTER\u0130LECEK HTML NESNES\u0130]&quot;&gt;\r\n<\/pre>\n<p>Burada &#8220;else&#8221; durumunda \u00e7al\u0131\u015facak html nesneleri olarak genellikle &lt;ng-template&gt; &lt;\/ng-template&gt; nesnesini tercih etmekteyiz.<br \/>\n\u00d6rnek olarak a\u015fa\u011f\u0131daki kod bloklar\u0131 &#8220;if &#8211; else&#8221; yap\u0131s\u0131n\u0131n kullan\u0131m\u0131 hakk\u0131nda daha netle\u015ftirici olacakt\u0131r.<\/p>\n<ul>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;true; else elseBlock&quot;&gt;\r\n  ...e\u011fer \u015fart do\u011fruysa...\r\n&lt;\/div&gt;\r\n&lt;ng-template #elseBlock&gt; ...e\u011fer \u015fart yanl\u0131\u015fsa...&lt;\/ng-template&gt;\r\n<\/pre>\n<p>if &#8220;true&#8221; de\u011ferine sahip oldu\u011fu i\u00e7in &#8220;&#8230;e\u011fer \u015fart do\u011fruysa&#8230;&#8221; de\u011feri yaz\u0131lacakt\u0131r.\n<\/li>\n<li>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-ng-if-example',\r\n  templateUrl: '.\/ng-if-example.component.html',\r\n  styleUrls: &#x5B;'.\/ng-if-example.component.css']\r\n})\r\nexport class NgIfExampleComponent implements OnInit {\r\n  HavaBulutluMu: boolean = false;\r\n  constructor() { }\r\n  ngOnInit() {\r\n  }\r\n}\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;HavaBulutluMu; else elseBlock&quot;&gt;\r\n  Bulutlu\r\n&lt;\/div&gt;\r\n&lt;ng-template #elseBlock&gt; Bulutsuz&lt;\/ng-template&gt;\r\n<\/pre>\n<p>&#8220;Bulutsuz&#8221; de\u011feri yaz\u0131lacakt\u0131r.\n<\/li>\n<\/ul>\n<h4><span style=\"color: #800080;\">ngIf ile If &#8211; Else If &#8211; Else Yap\u0131s\u0131<\/span><\/h4>\n<p>Ara\u015ft\u0131rmalar\u0131m neticesinde Angular 4 k\u00fct\u00fcphanesinde ngIf direktifine ait direkt olarak &#8220;if &#8211; else if- else&#8221; kal\u0131b\u0131nda bir yap\u0131 bulunmamaktad\u0131r. Bu y\u00fczden &#8220;else if&#8221; mant\u0131\u011f\u0131n\u0131 algoritmik olarak ger\u00e7ekle\u015ftirmekte ve a\u015fa\u011f\u0131daki mant\u0131kta \u00e7al\u0131\u015fmaktay\u0131z.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport { Component, OnInit } from '@angular\/core';\r\n@Component({\r\n  selector: 'app-ng-if-example',\r\n  templateUrl: '.\/ng-if-example.component.html',\r\n  styleUrls: &#x5B;'.\/ng-if-example.component.css']\r\n})\r\nexport class NgIfExampleComponent implements OnInit {\r\n  OdenenTutar: number = 1000;\r\n  constructor() { }\r\n  ngOnInit() {\r\n  }\r\n}\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;OdenenTutar &gt;= 250 &amp;&amp; OdenenTutar &lt;= 500;else elseifBlock1&quot;&gt;\r\n  {{OdenenTutar}} : &#x5B;250 ile 500] aras\u0131nda.\r\n&lt;\/div&gt;\r\n&lt;ng-template #elseifBlock1&gt;\r\n  &lt;div *ngIf=&quot;OdenenTutar &gt;= 500 &amp;&amp; OdenenTutar &lt;= 1000; else elseifBlock2&quot;&gt;\r\n    {{OdenenTutar}} : &#x5B;500 ile 1000] aras\u0131nda.\r\n  &lt;\/div&gt;\r\n&lt;\/ng-template&gt;\r\n&lt;ng-template #elseifBlock2&gt;\r\n  &lt;div *ngIf=&quot;OdenenTutar &gt;= 1000 &amp;&amp; OdenenTutar &lt;= 1500; else elseBlock&quot;&gt;\r\n    {{OdenenTutar}} : &#x5B;1000 ile 1500] aras\u0131nda.\r\n  &lt;\/div&gt;\r\n&lt;\/ng-template&gt;\r\n&lt;ng-template #elseBlock&gt;\r\n{{OdenenTutar}} : &#x5B;1500'den b\u00fcy\u00fck.\r\n&lt;\/ng-template&gt;\r\n<\/pre>\n<p>Yukar\u0131daki kod blo\u011funu incelerseniz e\u011fer, her bir else bir ba\u015fka if&#8217;e y\u00f6nlenmektedir. Yani farkl\u0131 bir \u015fekilde &#8220;if &#8211; else if &#8211; else&#8221; yap\u0131s\u0131 olu\u015fturmu\u015f bulunmaktay\u0131z.<\/p>\n<h4><span style=\"color: #800080;\">ngIf ile If &#8211; Then &#8211; Else Yap\u0131s\u0131<\/span><\/h4>\n<p>ngIf direktifinde \u015fart e\u011fer do\u011fruysa ngIf&#8217;in tan\u0131mland\u0131\u011f\u0131 html nesnesi d\u0131\u015f\u0131ndaki farkl\u0131 bir nesne \u00fczerinde \u00e7al\u0131\u015fma yapmak istiyorsan\u0131z &#8220;then&#8221; yap\u0131s\u0131n\u0131 kullanman\u0131z gerekmektedir.<\/p>\n<p>\u00d6rne\u011fin;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;div *ngIf=&quot;3 &gt; 2; then thenBlock; else elseBlock&quot;&gt;\r\n  if\r\n&lt;\/div&gt;\r\n&lt;ng-template #thenBlock&gt;then&lt;\/ng-template&gt;\r\n&lt;ng-template #elseBlock&gt;else&lt;\/ng-template&gt;\r\n<\/pre>\n<p>\u015feklinde bir \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirirsek \u015fart\u0131m\u0131z true oldu\u011fu zaman ekrana &#8220;if&#8221; de\u011fil &#8220;then&#8221; yaz\u0131s\u0131 yaz\u0131lacakt\u0131r.<\/p>\n<p>Evet&#8230;<br \/>\nAngular 4 k\u00fct\u00fcphanesine \u00f6zel ngIf direktifi \u00fczerine konu\u015facaklar\u0131m\u0131z yeterli oldu\u011fu i\u00e7in art\u0131k makalemizi burada noktalayabiliriz.<\/p>\n<p>Sonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde; Angular uygulamalar\u0131nda, yap\u0131sal olarak bildi\u011finiz klasik &#8220;if &#8211; else if &#8211; else&#8221; mant\u0131\u011f\u0131nda DOM nesnelerine belirli \u015fartlara g\u00f6re kontrol sa\u011flamaya ve m\u00fcdahalede bulunmaya yarayan ngIf direktifi \u00fczerine konu\u015fuyor olaca\u011f\u0131z. ngIf ile&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":7246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2144],"tags":[2262,2258,2261,2263,2260,2259],"class_list":["post-7633","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-4","tag-angular-4-if-else","tag-angular-4-ngif-direktifi","tag-angular-if-else","tag-angular-if-then-else","tag-angular-ngif","tag-ngif-direktifi"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=7633"}],"version-history":[{"count":0,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/7633\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/7246"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=7633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=7633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=7633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}