Shift the Kid Element to an Alternate Holder

Currently, I am working on a project in Angular version 10. Within this app, there is a component that can be shared and will utilize the provided content through ng-content. Typically, this content will consist of a list of items such as divs or buttons.

I am looking to implement logic that will relocate the last child of a container to another container based on specific conditions (specifically, if the width becomes too narrow, the items need to be moved elsewhere). I have already set up logic that saves the last item in an array and removes it from the original container. However, I am unsure of how to effectively add this element to the new container.
I attempted using 'innerHTML', but this displayed object text instead of an HTML element.
Similarly, trying to set the children of the new container to the moved items (either by pushing them or setting them to a new object) did not produce the desired outcome.
I am at a loss for what other options to explore.

If you would like to take a look, here is a quick StackBlitz demo I created: https://stackblitz.com/edit/angular-ivy-ioqhhx?file=src/app/app.component.html

Answer №1

If you're looking for a more traditional JavaScript solution rather than something specific to Angular, you can check out this code snippet: https://stackblitz.com/edit/angular-ivy-hi3cfd?file=src%2Fapp%2Fapp.component.ts. Any other suggestions using built-in Angular tools are welcome too.

moveLastItem() {
    const lastIndex = this.startHereDiv.nativeElement.children.length - 1;
    const child = this.startHereDiv.nativeElement.children[lastIndex];
    this.moveHereDiv.nativeElement.appendChild(child);
}

In case you're wondering why your original code doesn't work, it's because

this.startHereDiv.nativeElement.children[lastIndex]
returns the object representation of the element, while [innerHTML] expects a raw string representation. Check the console log for an example. If you still prefer the [innerHTML] solution, you'll need to bypass the Angular DOM sanitizer as demonstrated here: https://stackblitz.com/edit/angular-ivy-c7ylm1?file=src%2Fapp%2Fapp.component.ts,src%2Fapp%2Fapp.component.html

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

What is the method for accessing the string value of a component's input attribute binding in Angular 2?

In my Angular2 application, I have a straightforward form input component with an @Input binding pointing to the attribute [dataProperty]. The [dataProperty] attribute holds a string value of this format: [dataProperty]="modelObject.childObj.prop". The mod ...

The promise chain from the ngbModal.open function is being bypassed

I'm currently working on implementing data editing within a component. My task involves checking if any of the data fields have been altered, and if so, prompting a confirmation pop-up to appear. If the user confirms the change, the data will then be ...

Angular project hosting causing malfunctions in route parameters

Encountering a problem with route parameters after deploying my website on namecheap hosting. Routes Setup: const routes: Routes = [ { path: 'women', component: ProductlistingComponent }, { path: 'women/:search_1', component: ...

Concerns about unchanging Behavior Subject affecting Ionic 4 interface

I have created a list program using Ionic 4 and attempted to update the list by utilizing BehaviorSubject from rxjs. The list updates properly when initialized in the ngOnInit() method, but fails to update within the add() callback. Despite logging the ou ...

The object literal's property 'children' is assumed to have a type of 'any[]' by default

Is there a way to assign the property myOtherKey with any value? I encountered a Typescript error that says.. A problem occurred while initializing an object. The property 'children' in the object literal implicitly has an array type of 'a ...

Preventing me from instantiating objects

I've been struggling with an issue for a while now consider the following: export abstract class abstractClass { abstract thing(): string } export class c1 extends abstractClass { thing(): string { return "hello" } } export cla ...

having trouble transferring data from one angular component to another

I've been attempting to send data from one component to another using the service file method. I've created two components - a login component and a home component. The goal is to pass data from the login component to the home component. In the l ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

What is the procedure for cancelling a file upload in the FileUpload component of PrimeNG?

1. Issue Overview Looking to terminate file upload in PrimeNG's FileUpload component when certain filename patterns are detected. Using Angular 6.0.7 and PrimeNG 6.0.2. 2. Initial Strategy 2.1. HTML Code <p-fileUpload #fileUploader name="file" ...

Angular's change detection is currently inactive

I need to toggle the visibility of a button based on the value of a boolean variable using the Output property. However, I am facing an issue where the button remains hidden even after the variable is updated with a true value. Parent Component.ts showE ...

When utilizing the ngFor directive with the keyvalue pipe, an error occurs stating that the type 'unknown' cannot be assigned to the type 'NgIterable<any>'

I'm attempting to loop through this object { "2021-11-22": [ { "id": 1, "standard_id": 2, "user_id": 4, "subject_id": 1, "exam_date": "2021-11-22" ...

"Error: The property $notify is not found in the type" - Unable to utilize an npm package in Vue application

Currently integrating this npm package for notification functionalities in my Vue application. Despite following the setup instructions and adding necessary implementations in the main.ts, encountering an error message when attempting to utilize its featur ...

Angular2 - HTML not displaying the response

I am currently mastering angularjs2. In my latest project, I attempted to fetch data from an API and received a response successfully. However, I encountered an issue where the response is not rendering in homepage.component.html as expected. I am unsure o ...

Host app is failing to render shared components in SSR

Encountering an issue while implementing SSR with module federation? Check out my code example Steps to Begin: Run yarn install:all command Execute yarn shell:server:build task Start the server using yarn shell:server:start Initiate remote services with y ...

Tips for showing various tooltip text when iterating through a list?

I am currently working on a project where I am looping through a list and attempting to assign different tooltip text to various icons. However, I am struggling with the implementation. Here is a snippet of my code: <React.Fragment key={sv.key ...

Is it possible to combine TypeScript modules into a single JavaScript file?

Hey there, I'm feeling completely lost with this. I've just started diving into Typescript with Grunt JS and I could really use some assistance. I already have a Grunt file set up that runs my TS files through an uglify process for preparing the ...

Error in Angular Protractor e2e test due to iteration function on mat-radio-group innerText

clickOnQuestionsOption(optionName) { const radioButtonList = $$('mat-radio-group > mat-radio-button'); const selected_btn = radioButtonList.filter(elem => { return elem.getText().then(text => { console.log(tex ...

Can you explain how NGXS store manages its data storage? Is there a memory limit set for the NGXS store? And where exactly is the NGXS store memory located within the browser

Although I attempted to learn NGRX from their official site, I found myself lacking a clear understanding of where and how the data is stored within NGRX. Additionally, I could not find any information regarding the maximum limit of data that can be stor ...

How to bring in images from the assets folder using React and Typescript

I'm facing an issue where direct image importing is working, but when using object types, it's not functioning properly. Why could this be happening? I am currently working with "react": "^16.12.0" and "typescript": "~3.7.2" // ./src/assets/baby ...

What causes a delay in HTTP calls in Chrome when it is in the "Stalled" or "Initial Connection" state? Is it possible for these states to generate multiple threads for the same request?

My application uses Angular on the client side and Java (Spring Boot) on the backend. Occasionally, during some network calls, the waterfall chart gets stuck in either the "Stalled" or "Initial Connection" state. When this happens, I have noticed in my log ...