我正在使用Angular 5处理前端应用程序并使用后端的rest api。实际上,我正在开发管理平台,我有两个网页,一个用于显示客户列表,每个网页都有反馈列表,另外一个可以让您了解具体的反馈细节。
反馈详细信息会显示以下属性:account
,feedback
本身以及loyalty point
(如果存在)。
有两种方法,如果反馈有忠诚度点,那么反馈细节将显示忠诚度点值的详细信息,否则它将显示此属性的空输入,如果输入成功,它将返回更改值为{的主列表{1}}从false到true的反馈。
我正在使用rest api,为此操作我成功测试了API:
status
这是我的代码:
account.service.ts:
API: PATCH /Feedbacks/:id
component.html:
@Injectable()
export class AccountService {
constructor(private http: Http) {}
headers: Headers = new Headers({ 'Content-Type': 'application/json' });
options: RequestOptionsArgs = new RequestOptions({headers: this.headers});
// API: PATCH /Feedbacks/:id
updateStatus(feedback: Feedback) {
let url = "http://localhost:3000/api/Feedbacks";
return this.http.patch(url + feedback.id, feedback, this.options)
.map(res => res.json())
.catch(err => {
return Observable.throw(err)
});
}
}
component.ts:
<form *ngIf="feedback">
<div class="form-group">
<label for="InputAccount">Account</label>
<input type="text" class="form-control" id="InputAccount" value="{{feedback.account}}">
</div>
<div class="form-group">
<label for="InputFeedback">Feedback</label>
<textarea class="form-control" id="InputFeedback" rows="3" placeholder="Feedback">{{feedback.feedback}}</textarea>
</div>
<div class="form-group">
<label for="InputLP">LP</label>
<input type="text" class="form-control" id="InputLP" placeholder="LP" [(ngModel)]="account.lp" name="lp">
</div>
<div class="form-group" *ngIf="!edited; else showBack">
<button (click)="addLP(account,feedback)" class="btn btn-primary" data-dismiss="alert">Add LP</button>
</div>
</form>
反馈属性:
@Component({
selector: 'app-add',
templateUrl: './add.component.html',
styleUrls: ['./add.component.scss']
})
export class AddComponent implements OnInit {
feedback = {};
account = {};
edited:boolean;
status: boolean;
constructor(private route: ActivatedRoute, private accountService: AccountService,
private router: Router) { }
ngOnInit() {
this.route.paramMap
.switchMap((params: ParamMap) =>
this.accountService.getFeedback(+params.get('idF')))
.subscribe(feedback => this.feedback = feedback);
this.route.paramMap
.switchMap((params: ParamMap) =>
this.accountService.getAccount(params.get('idC')))
.subscribe(account => this.account = account);
}
addLP(account:Account,feedback:Feedback){
this.accountService.updateAccount(account)
.subscribe(res => {
this.account = res as Account;
console.log(res);
if (account.lp == null){
console.log(res);
this.edited = false;
} else {
this.edited = true;
this.accountService.updateStatus(feedback)
.subscribe(res => {
feedback.status = true;
this.feedback = res as Feedback;
console.log(this.feedback);
}, err => {
console.log(err);
});
}
}, err => {
console.log(err);
});
}
back() {
this.router.navigate(['list']);
}
}
帐户是帐户表的外键:
public id?: number,
public feedback?: string,
public account?: string,
public status?: boolean
当我尝试自动将状态值从false切换为true时,控制台日志将返回:
public account?: string,
public lp?: string
任何帮助将不胜感激!我真的需要解决它。感谢
答案 0 :(得分:0)
我修改了 account.service.ts:
中的代码updateStatus(feedback: Feedback) {
let url = "http://localhost:3000/api/Feedbacks";
var body = {status: true};
return this.http.patch(url +"/"+ feedback.id, body, this.options)
.map(res => res.json())
.catch(err => {
return Observable.throw(err)
});
}
它有效!