Angular2 is proving and ngClass directive as like Angular1. We can manipulate classes using this ngClass
Steps to add class using ngClass :
- Initiate this.myClass to array
- Push classes into above array
this.myClass = []; this.myClass.push('red');
Steps to remove class using ngClass :
- Initiate $scope.myClass to array
- Pop classes from above array
this.myClass = []; this.myClass.pop('red');
Steps to check class using ngClass :
- Initiate this.myClass to array
- Check index of the class that you want to check
this.myClass = ['red']; if(this.myClass.indexOf('red') == -1) { alert('false'); } else { alert('true'); }
Code for ngClass with Array
(function(app) { app.AppComponent = ng.core .Component({ selector: 'my-app', templateUrl: 'ngClass_template.html' }) .Class({ constructor: function() { this.myClass = []; }, addClass:function() { this.myClass.push('red'); }, removeClass:function() { this.myClass.pop('red'); }, checkClass:function() { if(this.myClass.indexOf('red') == -1) { alert('false'); } else { alert('true'); } } }); document.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(app.AppComponent); }); })(window.app || (window.app = {}));View DEMO
Steps to add class using ngClass with JSON
- Initiate this.myClass to JSON object of classes and boolean values
- Assign true for the JSON class name, that you want to add
this.myClass = {red:false}; addClass : function() { this.myClass.red = true; }
Steps to remove class using ngClass with JSON
- Initiate $scope.myClass to JSON object of classes and boolean values
- Assign false for the JSON class name, that you want to remove
this.myClass = {red:false}; removeClass : function() { this.myClass.red = false; }
Steps to toggle class using ngClass with JSON
- Initiate $scope.myClass to JSON object of classes and boolean values
- Assign opposite boolean value to the JSON class name
this.myClass = {red:false}; toggleClass : function() { this.myClass.red = !this.myClass.red; };
Code for ngClass with JSON
(function(app) { app.AppComponent = ng.core .Component({ selector: 'my-app', templateUrl: 'ngClass_template1.html' }) .Class({ constructor: function() { this.myClass = {red:false}; }, addClass:function() { this.myClass.red = true; }, removeClass:function() { this.myClass.red = false; }, toggleClass:function() { this.myClass.red = !this.myClass.red; }, checkClass:function() { alert(this.myClass.red); } }); document.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(app.AppComponent); }); })(window.app || (window.app = {}));
Angular 2 is providing one way binding for classes with [class.<class name>]. It binds the presence of the CSS class red on the element to the truthiness of the expression redClassBool. Observe the below code
<div id="divID" [class.red]="redClassBool"> This text color can be changed by class </div>
Add Class
Assign true to this.redClassBool
Remove Class
Assign false to this.redClassBool
Code for [class.] binding
(function(app) { app.AppComponent = ng.core .Component({ selector: 'my-app', templateUrl: 'withCssClass_template1.html' }) .Class({ constructor: function() { this.redClassBool = false; }, addClass:function() { this.redClassBool = true; }, removeClass:function() { this.redClassBool = false; }, toggleClass:function() { this.redClassBool = !this.redClassBool; }, checkClass:function() { alert(this.redClassBool); } }); document.addEventListener('DOMContentLoaded', function() { ng.platform.browser.bootstrap(app.AppComponent); }); })(window.app || (window.app = {}));View DEMO
Hi,
ReplyDeleteNice blog for adding / removing css
I need help on css in angular2
I have 3 links in the html and 3 links has some classes already and on top of that I need to
add 1 more class when I click on the particular anchor element i.e active element.
can you please help me on this.
Hello, I know it has been month till you write. I did this to solve that issue, maybe it helps someone:
Deleteimport {Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
template: '< div id="navbar" class="one two three" #navEle>'
})
export class AppComponent {
@ViewChild('navEle') navBar :ElementRef;
toggleNavbar() {
let oldClass :string = this.navBar.nativeElement.className;
this.navBar.nativeElement.className = oldClass.replace("three", "");
}
}
If someone didn't understand. I delete one class from the actual div, you could also add one. It's just a string object, you can replace, split, add etc. as you wish.
DeleteYou can check this link
Deletehttp://www.angulartutorial.net/2017/04/add-class-to-active-element-angular-2.html
YouTube Music MOD APK
DeleteYouTube Music MOD APK is a music player that lets you enjoy the countless music available on YouTube. This application offers a lot of useful features, including a playlist feature, which lets you add your favorite songs. The application is very easy to use and you can enjoy it in complete silence. Besides, it supports back-to-back play. If you want to try out YouTube, make sure that your device meets minimum system requirements.
YouTube Music mod apk is the perfect tool for those who love listening to music. Its database is almost identical to that of YouTube, so you'll find most music-related content in the search bar. There are also a lot of exciting features that you can enjoy with this application. If you'd like to get all the benefits of this app without paying any extra money, you can download the premium version of YouTube Music.
In addition to being a powerful music player, YouTube Music also lets you hide and flag inappropriate content. These features make the service far more enjoyable and accessible to users, ensuring that everyone can enjoy music. Normally, the premium version of YouTube can cost you money, but the premium version can be downloaded for free. If you want to try out premium versions of YouTube Music, download the premium mod. This app is a great way to get the latest music without paying anything.
Apart from being free, YouTube Music Mod APK also allows you to download songs to listen to them on the go. You can use it offline to listen to them at a later time. It's best to follow the instructions carefully and ensure that you don't do anything to break the app's compatibility. This is an excellent way to enjoy music without paying money. With this app, you'll be able to find the best songs without spending any money.
Another great feature of YouTube Music MOD APK is the ability to download songs. This enables you to listen to music without using data. You can also search for a song by line or lyrics. This is the most convenient way to enjoy your favorite songs. If you're a frequent listener of YouTube, this app will make your experience even better. You won't be interrupted with ads, so you can enjoy your music without distractions.
Using YouTube Music MOD APK is a great way to listen to music offline. It allows you to download and listen to songs without the need for an internet connection. Moreover, it also offers many features to make your experience on YouTube more pleasurable. With this application, you'll be able to download and play millions of songs. And while you're downloading them, you'll also be rewarded with a variety of other premium features.
Nice post buddy, this help me.
ReplyDeleteyour article is very good and very helpful. every one can use and know that how Creating Custom Animations for Use in Tableau. if you want to gif covert to image than check it you can easily understand that how to Use GIF Converter to Get Images for Marketing
ReplyDeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteIm grateful for the blog post.Really thank you!โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
สมัครสล็อตออนไลน์ >>> goldenslot
สนใจร่วมลงทุนกับเรา สมัครเอเย่น Gclub คลิ๊กได้เลย
Thanks For Sharing The Amazing content. I Will also share with my friends. Great Content thanks a lot
ReplyDeleteLove back Solution Specialist
Vashikaran specialist
Love Vashikaran Specialist
Vashikaran Specialist in India Vashikaran Specialist in Bangalore Vashikaran Specialist in Mumbai Love Vashikaran Specialist in MumbaiBlack magic specialist astrologer
great informative blog
ReplyDeleteCheck you Boards results here
Have you ever tried COC MOD Apk, which is the Mod version of its original server.
ReplyDeleteTrack your stocks in all major markets instantly with our unique live stock overview. See Ftoc Stock live prices changes as they occur and view change details, including volume and share changes.
ReplyDeleteHello.This post was extremely interesting, particularly because I was looking for thoughts on this topic last Thursday. Regards GBWhatsapp APK
ReplyDeleteDo you want to consult about your company's cloud services, contact 0 scale as 0scale is an AWS Premier Consulting Partner with 11 AWS expertise that offers in-depth technical skills to enable organizations to modernize on the cloud. We are skilled at designing, executing, optimizing, and handling customized cloud solutions on AWS that coerce the innovation our clients require to achieve their business transformation initiatives. 0scale
ReplyDeleteOggi ti presento un sito Web sulle ultime suonerie-mp3-gratis.com.
ReplyDelete