Encountering an error when utilizing Angular Animation with Bootstrap -> Issue arises from attempting to read properties of undefined (specifically 'box-sizing')

To access the source code of Git, visit https://github.com/codezj/exampleAppAnimation.

I recently developed a method to extract Bootstrap CSS and utilize it as CSS in Angular. However, I encountered an error while running the Angular project: Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'box-sizing')

Here are the snippets of code that may be causing the issue:

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")),


])

Despite thorough output logs, I have yet to pinpoint the exact cause of the issue.

Answer №1

This error is occurring because in the following line of code:

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

when index = 'box-sizing', you are attempting to access this property in the undefined object result.

To resolve this issue, you can assign a value to it based on the intended use of the variable. For example:

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

Alternatively, you can initialize result as an empty array, uncomment

result[index] = styleRule.style[index];
, and place it before the console.log statement:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

angular global search is only effective without any filters applied

I am facing an issue with my global search function. It works fine when I display the data directly from the controller. However, the problem arises when I apply a filter - the search function no longer detects the data. For example, if I have a date time ...

What is the method for inserting a specific index into an interface array in TypeScript?

In my angular(typescript) application, I have an interface defined as follows: export interface PartnerCnic{ id: string; shipperRegCnicFront: File; shipperRegCnicBack: File; } Within my component, I have initialized an empty array for this interface li ...

Challenges of relative positioning with dynamic height based on content

Hello! I'm trying to achieve this https://i.stack.imgur.com/Q6lXP.png In this case, there are 3 divs. The top one will have a width of 100% and a height of auto, with relative positioning. The second div is similar, but I plan to add some dummy text ...

Google Calendar iFrame experiencing overflow issues after rotation on iOS devices

I've managed to successfully embed a Google calendar into a website using an iframe. Everything is scaling properly on various browsers, however, I've encountered an issue with the calendar overflowing vertically when rotating an iPhone from port ...

Conceal content within a bullet point

I'm encountering an issue with this specific element. HTML <div class="navbar slide-menu"> <div class="container"> <ul class="nav navbar-default"> <li class="brand"><a href="#">Menu</a></li> ...

Angular signal failed to update after the next render cycle

When utilizing angular SSR, why is it necessary to manually initiate a change detection when using afterNextRender? Considering this is an angular API. Is there a workaround for this issue? afterNextRender(() => { this.someAPI.then( ...

Image expansion

I have a container element div that holds various content of unknown length. How can I add a background image that extends the entire length of the container since background-images do not stretch? I attempted to use a separate div with an img tag inside. ...

Ways to secure a floating item onto a backdrop picture

Currently I am working on editing this page. When you hover over the cat, its eyes blink or two black dots randomly appear. My goal is to prevent the random blinking dots from appearing. I want to keep the hover object attached to the cat/background image ...

Steps to fix the issue of 'Property 'foo' lacks an initializer and is not definitely assigned in the constructor' while utilizing the @Input decorator

What is the proper way to initialize a property with an @Input decorator without compromising strict typing? The code snippet below is triggering a warning: @Input() bar: FormGroup = new FormGroup(); ...

How can I achieve a smooth background-image transition in Firefox?

Currently, I'm on the hunt for a substitute for this specific code snippet: "transition:background-image 1s whatever" This particular transition only seems to function on webkit browsers, leaving Firefox users out of luck. I experimented with utili ...

A comprehensive guide to using Reactive Forms in Angular

I need help understanding how FormGroup, FormControl, FormArray work in Angular. The error message I'm encountering is: Type '{ question: FormControl; multi: true; choices: FormArray; }' is not assignable to type 'AbstractControl' ...

Encountering numerous issues during my attempt to perform an npm install command

After cloning a git repository, I encountered an issue when trying to run the app in the browser. Despite running "npm install," some dependencies were not fully installed. Upon attempting to run "npm install" again, the following errors were displayed: np ...

Is there a way to make my item reach a height of 100%?

I've created a unique Wordpress theme and I'm looking to make an element 100% height to fill the remaining available space. Here is the HTML code snippet: <header id="masthead" class="navbar navbar-default" role="banner" style=" backgrou ...

Designing the child table layout using grid grouping in ExtJS4

I have customized my Extjs4 grid grouping implementation to hide the expand-collapse sign and prevent group header expansion for empty groups. However, I am facing an issue where a white 1px border is being created due to the following CSS class: .x-grid- ...

Is there anyone who can provide a straightforward solution (code) for < something basic?

I have learned a lot about programming, but I'm stuck on one thing. How can I make an image stay in place when clicked on? What I mean is that when you click and hold on the image, then move the cursor, the image moves with the cursor. What I want is ...

Troubleshooting code: JavaScript not functioning properly with CSS

I am attempting to create a vertical header using JavaScript, CSS, and HTML. However, I am facing an issue with the header height not dynamically adjusting. I believe there might be an error in how I am calling JSS. Code : <style> table, tr, td, t ...

Is there a conflict between bootstrap.min.JS and chart.min.js?

I have been working on developing an admin page for customer support and I recently added a visually appealing chart to display some data on the home screen. The chart integration was successful, but when I introduced tab panes to the page and refreshed ...

Discovering the true rendering elements in karma tests: A guide

Recently, I started working on a new project in Angular14. However, when I completed writing the unit tests, all I could see was a series of successful text information and not any HTML elements displayed. How can I view the HTML elements? ...

The TreeView control displays as a <div> element, which results in an unexpected line break

I am currently working on designing a layout that includes an ASP.NET TreeView control on the left-hand side. However, I am facing an issue where the TreeView renders as a div, causing a line break. I have tried using display:inline, but it doesn't se ...

A guide on iterating through various JSON arrays and displaying them in AngularJS

I'm in the process of developing a new school management system and I need to showcase the list of departments along with the courses being offered by each department. The course information is saved in JSON format within the database. Here is my JSO ...