Monday, April 23, 2018

How do I conditionally apply CSS styles in AngularJS?



Q1. Suppose I want to alter the look of each "item" that a user marks for deletion before the main "delete" button is pressed. (This immediate visual feedback should eliminate the need for the proverbial "are you sure?" dialog box.) The user will check checkboxes to indicate which items should be deleted. If a checkbox is unchecked, that item should revert back to its normal look.



What's the best way to apply or remove the CSS styling?



Q2. Suppose I want to allow each user to personalize how my site is presented. E.g., select from a fixed set of font sizes, allow user-definable foreground and background colors, etc.




What's the best way to apply the CSS styling the user selects/inputs?


Answer



Angular provides a number of built-in directives for manipulating CSS styling conditionally/dynamically:




  • ng-class - use when the set of CSS styles is static/known ahead of time

  • ng-style - use when you can't define a CSS class because the style values may change dynamically. Think programmable control of the style values.

  • ng-show and ng-hide - use if you only need to show or hide something (modifies CSS)

  • ng-if - new in version 1.1.5, use instead of the more verbose ng-switch if you only need to check for a single condition (modifies DOM)


  • ng-switch - use instead of using several mutually exclusive ng-shows (modifies DOM)

  • ng-disabled and ng-readonly - use to restrict form element behavior

  • ng-animate - new in version 1.1.4, use to add CSS3 transitions/animations



The normal "Angular way" involves tying a model/scope property to a UI element that will accept user input/manipulation (i.e., use ng-model), and then associating that model property to one of the built-in directives mentioned above.



When the user changes the UI, Angular will automatically update the associated elements on the page.






Q1 sounds like a good case for ng-class -- the CSS styling can be captured in a class.

ng-class accepts an "expression" that must evaluate to one of the following:




  1. a string of space-delimited class names

  2. an array of class names

  3. a map/object of class names to boolean values




Assuming your items are displayed using ng-repeat over some array model, and that when the checkbox for an item is checked you want to apply the pending-delete class:




... HTML to display the item ...




Above, we used ng-class expression type #3 - a map/object of class names to boolean values.






Q2 sounds like a good case for ng-style -- the CSS styling is dynamic, so we can't define a class for this.

ng-style accepts an "expression" that must evaluate to:




  1. an map/object of CSS style names to CSS values



For a contrived example, suppose the user can type in a color name into a texbox for the background color (a jQuery color picker would be much nicer):





...





Fiddle for both of the above.

The fiddle also contains an example of ng-show and ng-hide. If a checkbox is checked, in addition to the background-color turning pink, some text is shown. If 'red' is entered in the textbox, a div becomes hidden.



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...