{"id":11266,"date":"2024-09-23T12:41:32","date_gmt":"2024-09-23T12:41:32","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=11266"},"modified":"2024-09-24T04:58:20","modified_gmt":"2024-09-24T04:58:20","slug":"ng-class-in-angularjs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/angular\/ng-class-in-angularjs","title":{"rendered":"AngularJS ng-class"},"content":{"rendered":"<h2>What is ng-class in AngularJs?<\/h2>\n<p>The ng-class is an inbuilt directive of angularJS that allows users to dynamically bind css class to html element based on some condition.<\/p>\n<p>The defined css class will be automatically assigned to elements whenever condition becomes true and also removed if condition becomes false.<\/p>\n<p>So the user has not to worry about adding \/removing the css class again once,based on the condition that the resulting class will be added and removed.<\/p>\n<p>Here the condition can be any type i.e on object value, array, number,string etc and based on the condition expression\u2019s resultI(boolean) the class will be added and removed.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n<HTMLtag ng-class=\u201d{\u2018cssClassName1\u2019:condition1, \u2018cssClassName2\u2019:condition2}\u201d\/>\r\n<\/pre>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n<div ng-class=\"{\u2018highlight\u2019:2===1}\"><\/div>\r\n<\/pre>\n<p><strong>Css:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n.highlight {  border:2px solid red;  color:blue  }\r\n<\/pre>\n<h3>Output:<\/h3>\n<p>Here the div will have css class highlight assigned as the condition is true.<\/p>\n<p>If we change the html to\u2026<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n <div ng-class=\"{\u2018highlight\u2019:2===1}\"><\/div>\r\n<\/pre>\n<p>Then the highlight class will not be assigned to div as the condition result is false.<\/p>\n<p>So we can see here using ng-class we can add css class to html elements conditionally<\/p>\n<h3>Complete Example:<\/h3>\n<p><strong>Scenario:<\/strong><br \/>\nAn example to show a list of people for selection, and on selection of any person name from the selection list, the Title and information text style will be updated with ng-class directive\u2026<\/p>\n<p><strong>HTML:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n<script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.2.12\/angular.min.js\"><\/script>\r\n<div ng-app=\"myApp\">\r\n  <div class=\"container\" ng-controller=\"Ctrl_List\">\r\n    <div class=\"row\">\r\n\t      <div class=\"col-md-12 pb\" ng-class=\"{'highlight-title':name}\">\r\n\t        <h1>AngularJS ng-class Example<\/h1>\r\n\t        <small ng-class=\"{'highlight-info':name}\">I will be highlighted if any person from dropdown gets selected by user<\/small>\r\n\t      <\/div>\r\n    <\/div>\r\n    <div class=\"row\">\r\n\t      <div class=\"col-md-6\">\r\n\t        <h4>Person name<\/h4>\r\n\t        <select ng-model=\"name\" multiple=\"multiple \" size=\"5\" ng-options=\"l.name for l in people\" class=\"col-md-12\"><\/select>\r\n\t      <\/div>\r\n    <\/div>\r\n  <\/div>\r\n<\/div>\r\n<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/09\/23124106\/ngclass-angular-1024x949.jpg\" alt=\"ngclass-angular\" width=\"1024\" height=\"949\" class=\"alignnone size-large wp-image-11282\" srcset=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/09\/23124106\/ngclass-angular-1024x949.jpg 1024w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/09\/23124106\/ngclass-angular-300x278.jpg 300w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/09\/23124106\/ngclass-angular-768x711.jpg 768w, https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2024\/09\/23124106\/ngclass-angular.jpg 1100w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<h3>Other Syntax for ng-class<\/h3>\n<p><code><HTMLtag ng-class=\u201d{\u2018cssClassName1 cssClassName2\u2018:condition}\u201d\/><\/code><br \/>\n<strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\"><div ng-class=\"{\u2018highlight-title highlight-info\u2019:1==1}\"><\/div><\/pre>\n<p>This is shorthand syntax for scenarios when we want to add multiple classes on elements based on the same condition.<br \/>\nif we don\u2019t use above shorthand syntax then we have to write code like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\"><div ng-class=\"{highlight-title\u2019:1==1,\u2019highlight-info\u2019:1==1}\"><\/div><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is ng-class in AngularJs? The ng-class is an inbuilt directive of angularJS that allows users to dynamically bind css class to html element based on some condition. The defined css class will be automatically assigned to elements whenever condition becomes true and also removed if condition becomes false. So the user has not to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11267,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-11266","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11266"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=11266"}],"version-history":[{"count":15,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11266\/revisions"}],"predecessor-version":[{"id":11292,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/11266\/revisions\/11292"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/11267"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=11266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=11266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=11266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}