Home > OS >  Use Angular Animation with Bootstrap encounters an error -> Cannot read properties of undefined (
Use Angular Animation with Bootstrap encounters an error -> Cannot read properties of undefined (

Time:12-24

source code of git -> https://github.com/codezj/exampleAppAnimation.

I have created a method to read bootstrap css and use them as css in Angular. But it raised an error when i run the project of angular -> Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'box-sizing')

codes -> animationUtils.ts ->

export function getStylesFromClasses(names: string | string[],

    elementType: string="div") : {[key: string]: string | number}
    
    
    {
        let elem = document.createElement(elementType);
        console.log(elem);
        
        (typeof names == "string" ? [names]:names).forEach(c => elem.classList.add(c));
        let result : any;
        for (let i =0; i < document.styleSheets.length; i  ){
            let sheet = document.styleSheets[i] as CSSStyleSheet;
            
            let rules = sheet.rules || sheet.cssRules;
            console.log(rules,'rules rulesrulesrulesrules');
            for (let j =0; j< rules.length; j  ){
                if(rules[j].type == CSSRule.STYLE_RULE){
                    let styleRule = rules[j] as unknown as CSSStyleSheet;
                    console.log(styleRule,'styleRules----styleRulestyleRule');
                    if ( styleRule instanceof CSSStyleRule){
                        
                        if (elem.matches(styleRule.selectorText)){
                            for (let k = 0; k < styleRule.style.length; k  ){
                                console.log(k,'k kkkkkk');

                                
                                let index: any = styleRule.style[k]
                                console.log(styleRule.style[k],result[index],styleRule.style[index] );
                                
                                // result[index] = styleRule.style[index];
                            }
    
                        }
                    }
                    
                }
            }
        }

        return result;
    

    }

table.animations.ts ->

import {trigger, style, state, transition, animate, group} from "@angular/animations"
import { bindCallback } from "rxjs"
import { getStylesFromClasses } from "./animationUtils"




const commonStyles = {
    border: "black solid 4px",
    color: "white"
}

export const HighlightTrigger = trigger("rowHightlight",[


    // state("selected", style([commonStyles,{
    //     backgroundColor: "lightgreen",
    //     fontSize:"20px"
    // }])),
    
    // state("notselected", style([commonStyles,{
    //     backgroundColor: "lightsalmon",
    //     fontSize:"12px",
    //     color: "black"
    // }])),

    state("selected",style(getStylesFromClasses(["bg-success"]))),
    // state("notselected",style(getStylesFromClasses(["bg-info"]))),


    state("void", style({
        
        transform: "translateX(-50%)"
    })),

    transition("* => notselected", animate("200ms")),
    transition("* => selected", [animate("400ms 200ms ease-in",
    
                style({
                    backgroudColor: "lightblue",
                    fontSize: "25px"
                })),
                animate("250ms",style({
                    backgroudColor: "lightcoral",
                    fontSize: "30px"
                })),
                group([
                    animate("250ms", style({
                        backgroundColor: "lightcoral",
                    })),

                    animate('450ms', style({fontSize:"30px"})),
                ]),

                animate("200ms")

            ]
                
                
                
                
                ),
    transition("void => *", animate("500ms")),


])

I tried output logs as much as possible but not helpful.

CodePudding user response:

As the error says, you get this error because in this line:

 console.log(styleRule.style[k],result[index],styleRule.style[index]);

with index = 'box-sizing', you are trying to read this property in the object result which is undefined.

You should assign it a value, depending on the purpose of the use of this variable you can do:

  ...
  if ( styleRule instanceof CSSStyleRule){
        result = styleRule.style;
        if (elem.matches(styleRule.selectorText)){
  ...

Or initialize result with result = [], uncomment result[index] = styleRule.style[index]; and put it before the console.log:

 ...
  let index: any = styleRule.style[k]
  result[index] = styleRule.style[index];                         
  console.log(styleRule.style[k],result[index],styleRule.style[index] );
 ...
  • Related