Hi i am a student developer. I have a question. On Internet, there are so many resources about it but many of them are about AngularJS. With this case, I did not find a clear solution to my project. When I make delete or add data to my Api, How can I add or delete html element without reload web page? Do you have an advice to remove or add in tweetDisplay element ?
My Html page;
<form #tweetAddForm="ngForm" (ngSubmit)="addTweet(tweetAddForm)">
<div class="tweetPart">
<div><img src="../../../assets/img/r1.jpg" class="profileImg rounded-circle" alt="Profile Img" /></div>
<div class="tweetItem">
<mat-form-field class="tweetArea">
<textarea id="tweetContent" #tweetForSend="ngModel" [(ngModel)]="tweetModel.tweetContent" matInput
placeholder="Neler Oluyor?" required name="tweetContent"></textarea>
</mat-form-field>
</div>
</div>
<div class="iconsAndBtn"><button mat-button class="btnTweetSend" [disabled]="tweetAddForm.invalid"
type="submit">Tweetle</button>
</div>
</form>
<div *ngFor="let tweet of tweets" class="tweetDisplay ml-2">
<div class="tweetterInfo">
<div class="ml-2"><b>Emre Sert</b> @emreeseert</div>
<div class="ml-2">{{tweet.tweetContent}}</div>
<i class="far fa-edit ml-4"></i>
<i style="cursor: pointer;" (click)="removeSelectedTweet(tweet)" class="fas fa-trash-alt ml-5"</i>
</div>
</div>
Component.ts;
import { Component, OnInit } from '@angular/core';
import { Tweet } from './tweet'
import { TweetService } from './tweet.service';
import { NgForm } from '@angular/forms';
declare let alertify:any;
@Component({
selector: 'app-tweet',
templateUrl: './tweet.component.html',
styleUrls: ['./tweet.component.css'],
providers:[TweetService]
})
export class TweetComponent implements OnInit {
tweets: Tweet[];
tweetModel : Tweet = new Tweet();
constructor(private tweetService :TweetService ) {}
ngOnInit() {
this.tweetService.getTweets().subscribe(data=>{
this.tweets = data
})
}
addTweet(form:NgForm){
this.tweetService.addTweet(this.tweetModel).subscribe(data=> alertify.error('added.'));
}
removeSelectedTweet(tweet:Tweet){
if(confirm("Are you sure ?")){
this.tweetService.removeSelectedTweet(tweet.tweetId).subscribe(data=> alertify.error('deleted'));
}
}
}