Reputation: 891
My RouteParams didn't work well when I click "edit" button.
in the first view I click the edit btn on the bob row
and this is the second view
now i click the back button in the browser and it goes back to fist view. then i click the edit btn on the pop row and this is the third view(same as second view which is wrong)
and I put some console to debug. this is the console log
here is my code for the first view when i click the edit button, it fire the func and record the 1 and 3 records in the console.log picture
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import { Router, RouteParams, ROUTER_DIRECTIVES } from 'angular2/router';
import {MailingListService} from'../../services/mailing-list/mailing-lists.service'
import {Subscriber} from '../../models/subscriber/subscriber';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'subscriber-list-edit-view',
templateUrl: './app/components/subscriber-list-editor-view/subscriber-list-editor-view.html',
directives: [ROUTER_DIRECTIVES]
})
export class EditSubscriberListViewComponent {
subscribers: Subscriber[];
id: string;
constructor(private mailingListsService: MailingListService, params: RouteParams, private router: Router) {
this.init(this.id = params.get("id"));
}
init(id: string) {
this.mailingListsService.getSubscribers(this.id).subscribe(x => {
this.subscribers = x;
});
}
//this is fired when click the edit button
editSubscriber(subscriber: Subscriber) {
this.router.navigate(['Subscribers.Edit', { id: this.id, subscriber: subscriber }]);
console.log(subscriber);//this print the first and third record in console.log
}
deleteSubscriber(subscriber: Subscriber) {
this.mailingListsService.deleteSubscriber(this.id, subscriber)
.subscribe(() => { this.init(this.id) });
}
}
here is my code for the second view after i click the edit button and jump to the edit view, it fire the constructor and record the 2 and 4 records in the console.log picture
import {Component} from 'angular2/core';
import { Router, RouteParams } from 'angular2/router';
import {CORE_DIRECTIVES} from 'angular2/common';
import {Subscriber}from '../../models/subscriber/subscriber';
import {MailingListService} from'../../services/mailing-list/mailing-lists.service'
@Component({
selector: 'create-subscriber-view',
templateUrl: './app/components/subscriber-creator-view/subscriber-creator-view.html'
})
export class CreateSubscriberComponent {
subscriber: Subscriber;
id: string;
subscriberId: string;
name: string;
phoneNumber: string;
email: string;
isActivated: boolean;
isBusy: boolean;
isEdit: boolean;
constructor(private mailingListService: MailingListService,private params: RouteParams, private router: Router) {
this.id = params.get("id");
this.subscriber = params.get("subscriber");
console.log(params.get("subscriber")); //this doesn't work well,print the second and forth record in the console.log
if (this.subscriber != null) { this.isEdit = true; this.name = this.subscriber.name; this.phoneNumber = this.subscriber.phoneNumber; this.email = this.subscriber.email; this.subscriberId = this.subscriber.id; this.isActivated = this.subscriber.isActivated;};
}
create() {
if (this.phoneNumber == null && this.email == null) { alert("either phone or email is required ! "); return; }
this.isBusy = true;
this.mailingListService.createSubscriber(this.id, {
name: this.name,
phoneNumber: this.phoneNumber,
email: this.email
})
.subscribe(() => { this.router.navigate(['MailingLists.View', { id: this.id }]) });
}
edit() {
if (this.phoneNumber == null && this.email == null) { alert("either phone or email is required ! "); return; }
this.isBusy = true;
this.mailingListService.editSubscriber(this.id, {
name: this.name,
phoneNumber: this.phoneNumber,
email: this.email
})
.subscribe(() => { this.router.navigate(['MailingLists.View', { id: this.id }]) });
}
}
as you can see, the fourth record in console.log is not correct so i assume it is the params problem. but i don't know why and how to solve the problem.
and this is the routeconfig
{ path: '/MailingLists/:id/Subscribers/Create', component: CreateSubscriberComponent, as: 'Subscribers.Create' },
{ path: '/MailingLists/:id/Subscribers/Edit', component: CreateSubscriberComponent, as: 'Subscribers.Edit' },
create and edit use same component but different url.
Upvotes: 0
Views: 301
Reputation: 891
Because previously I pass my subscriber as an object and routeparams find the object by reference. That maybe the reason why it didn't refresh the data.
I have a working solution here. Simply do a JSON.stringify(subscriber) instead pass an object you pass a string.
editSubscriber(subscriber: Subscriber) {
this.router.navigate(['Subscribers.Edit', { id: this.id, subscriberString: JSON.stringify(subscriber) }])//here
}
now, in the second component you get the string and do a JSON.parse()
constructor(private mailingListService: MailingListService, private params: RouteParams, private router: Router) {
this.id = params.get("id");
this.subscriberString = params.get("subscriberString");
if (this.subscriberString != null) {
this.isEdit = true;
this.subscriber = JSON.parse(this.subscriberString);//Here
this.name = this.subscriber.name;
this.phoneNumber = this.subscriber.phoneNumber;
this.email = this.subscriber.email;
this.subscriberId = this.subscriber.id;
this.isActivated = this.subscriber.isActivated;
};
}
Always remember your routeparams can be only type of string number boolean (primitive types?)
Upvotes: 0