admin管理员组

文章数量:1023009

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-modalponent

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: './testponent.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 ponent 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 e back

... is not a function

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.ponent

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.ponent.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 ponent 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 e back

... is not a function

Share Improve this question edited Aug 1, 2018 at 13:22 murday1983 asked Oct 17, 2017 at 10:49 murday1983murday1983 4,03617 gold badges62 silver badges111 bronze badges 8
  • could you show how you implemented calling this function in html? – Jaroslaw K. Commented Oct 17, 2017 at 10:58
  • Use this.ref.detectChanges(); And import: import { ChangeDetectorRef } from '@angular/core'; Documentation: angular.io/api/core/ChangeDetectorRef – Amit Commented Oct 17, 2017 at 11:19
  • can you share html codes ? – Chandru Commented Oct 17, 2017 at 11:32
  • @SurenSrapyan splice or indexof as per my mented out code – murday1983 Commented Oct 17, 2017 at 11:54
  • @JaroslawK HTML added – murday1983 Commented Oct 17, 2017 at 11:57
 |  Show 3 more ments

3 Answers 3

Reset to default 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 bine 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/docs/handbook/interfaces.html

so it bees

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 mon typescript behaviours for angular2/4/5 so we are hoping to bee more used to them when we have hads more time to use them!

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-ponent [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.

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 ponent 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));
    ponent2.deleteFunction();
}

Component2.ts

Then we can pass our identifiers into our parent or child ponents 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.

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-modalponent

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: './testponent.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 ponent 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 e back

... is not a function

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.ponent

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.ponent.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 ponent 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 e back

... is not a function

Share Improve this question edited Aug 1, 2018 at 13:22 murday1983 asked Oct 17, 2017 at 10:49 murday1983murday1983 4,03617 gold badges62 silver badges111 bronze badges 8
  • could you show how you implemented calling this function in html? – Jaroslaw K. Commented Oct 17, 2017 at 10:58
  • Use this.ref.detectChanges(); And import: import { ChangeDetectorRef } from '@angular/core'; Documentation: angular.io/api/core/ChangeDetectorRef – Amit Commented Oct 17, 2017 at 11:19
  • can you share html codes ? – Chandru Commented Oct 17, 2017 at 11:32
  • @SurenSrapyan splice or indexof as per my mented out code – murday1983 Commented Oct 17, 2017 at 11:54
  • @JaroslawK HTML added – murday1983 Commented Oct 17, 2017 at 11:57
 |  Show 3 more ments

3 Answers 3

Reset to default 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 bine 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/docs/handbook/interfaces.html

so it bees

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 mon typescript behaviours for angular2/4/5 so we are hoping to bee more used to them when we have hads more time to use them!

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-ponent [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.

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 ponent 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));
    ponent2.deleteFunction();
}

Component2.ts

Then we can pass our identifiers into our parent or child ponents 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.

本文标签: javascriptAngular 2Typescript Delete Object On Button ClickStack Overflow