1

I have an Angular 2 app using Typescript but i am new to this, what i have is a table with a 'Delete' button,

I can pass the object data to my confirmation modal but when i 'Confirm' it, its still in my table.

delete-modal.component

import { Component, OnInit, Inject, Input } from '@angular/core';
import { TestService } from '../../ABC/TestService/TestService.service';
import { MdDialog, MdDialogRef, MD_DIALOG_DATA } from '@angular/material';

import { testModal } from 'models/test';

@Component({
    selector: 'app-test',
    templateUrl: './test.component.html',
    styleUrls: ['./test.css']
})

export class testDeleteModalComponent implements OnInit {
    @Input('test') test: testModal;

    constructor(private TestService: TestService, private accountService: AccountService,
        @Inject(MD_DIALOG_DATA) private dialogData: any) { }

    ngOnInit() {
        console.log('test', this.dialogData.beneficiary);
        this.test = this.dialogData.test;
    }

    deleteTest() {

        if (this.dialogData.test.identifier) {
            // this.dialogData.beneficiary.splice(this.dialogData.beneficiary.indexOf(this.beneficiaryAnt), 1);
            // this.dialogData.beneficiary.splice(this.beneficiary);
            // delete this.beneficiary;
            this.dialogData.test.splice(this.dialogData.test.indexOf(this.dialogData.test), 1);
        } else {
            this.dialogData.test.operation = 'X';
        }
    }
}

HTML

<button md-icon-button (click)="deleteTest()" name="deleteTestDetails">
    <md-icon>delete forever</md-icon>
</button>

All other HTML is in a main component and the 'Delete' button is used as shown below

<app-test-main-page-delete-button [test]="test"></app-test-main-page-delete-button>

The 'deleteTest' method is called when the user click the confirm button.

I have also included above some ways i have tried in the IF but they always come back

... is not a function

8
  • could you show how you implemented calling this function in html? Commented Oct 17, 2017 at 10:58
  • Use this.ref.detectChanges(); And import: import { ChangeDetectorRef } from '@angular/core'; Documentation: angular.io/api/core/ChangeDetectorRef Commented Oct 17, 2017 at 11:19
  • can you share html codes ? Commented Oct 17, 2017 at 11:32
  • @SurenSrapyan splice or indexof as per my commented out code Commented Oct 17, 2017 at 11:54
  • @JaroslawK HTML added Commented Oct 17, 2017 at 11:57

3 Answers 3

5

It is good that you asked this question, my projects of three peoples also struggling with this. we have found is two ways. what i will show is two ways of doing typescriptdelete.

solution a. because it is object, it will need identifier. First is

var objectdelete = {
    identifier: 'Mydelte',
    value: '168%'
}

Next what we need is now service. some people call them directives but from my experience they are the same thing. We have alert so user knows if they did not set identifier that they must go back. I do not see service on your side, i see array being deleted. if you combine the array and the service, this will then be working across whole website.

export class DeleteService

delete(objectToDelete: string) {

    if (!objectToDelete.identifier) {
       alert('No identifer');
    }else {
        // Delete from your array here.
    }
}

Solution 2.

If the above does not meed your needs, our tema also experimented with interfaces in typescript. You can see them here https://www.typescriptlang.org/docs/handbook/interfaces.html

so it becomes

export class myDeleteService {
    deleter: IDeleter
}

export interface IDeleter {
    delete: this.delete.delete(deletE);
    deleteArray: this.array =[];
}

then simply in your html it will be

<button (click)='delete(dieleter)'>Delete me!</button>

These are all common typescript behaviours for angular2/4/5 so we are hoping to become more used to them when we have hads more time to use them!

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way to delete data object on button click and refresh instantly when it's done :

Your parent html has to call children like this :

<app-component [inputData]="dataTable" (inputDataChange)="resetData()"/>

Add dataTable as class variable and implement the output function :

resetData() { this.dataTable=[] }

Then in children html leave your code (you can use this changes)

<button class="fa fa-delete" (click)="deleteTest()" name="deleteTestDetails">Delete</button>

Finaly in your children ts file set your data object for each change, and implement your input function

myDataTable: any = [];
@Input set inputData(data: DataTable) {
   if(data) {
     this.myDataTable = data;
}}

@Output() inputDataChange: EventEmitter<any> = new EventEmitter();

deleteTest() {
   this.inputDataChange.emit(true); 
}

What does this code do ? It will emit and event to the parent when the delete button is clicked, then your parent will delete the dataTable, and finally, your children input will refresh it, as setter will catch the changes and refresh the variable.

If you want to apply those rules to table changes, then simply emit your dataTable and reassign it instead of reset it.

Comments

-1

I am in a project with and our team have struggled on this for a whiles.

First thing I will say is this, Angular has not made this an easy task, so we will attempt to ignore the framework and write pure Java instead to make our lives easyer on ourselves.

SO looking at your button, I can see that you have started on the right track.

If the button is calling your component like the following

Html/Java

<button ng-click="delete()">Click me<button>

Component.ts

function delete = deleteMethod(testIdentifier) {
    var abc = this.beneficiary.beneficiaryIdentifier.test.splice(this.beneficiary.beneficiaryIdentifier.test.indexOf(testIdentifier));
    component2.deleteFunction();
}

Component2.ts

Then we can pass our identifiers into our parent or child components and remove the beneficiary like so:

deleteMethod(deetle) {
    this.beneficiary.removeAtIndex(testIdentifier.splice(1), 1);
}

Nice and easy looking back, but it took our team of threes a long whiles to figure that ones out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.