Javascript *ngIf会创建一个无限循环,并且永远不会解析
我在角度上工作,只想在某些值对齐时显示某种形式。我有后端路线的工作和测试与邮递员。这是我的密码 大宗报价 角度组件HTMLJavascript *ngIf会创建一个无限循环,并且永远不会解析,javascript,angular,typescript,express,Javascript,Angular,Typescript,Express,我在角度上工作,只想在某些值对齐时显示某种形式。我有后端路线的工作和测试与邮递员。这是我的密码 大宗报价 角度组件HTML 您在canEdit()方法中返回的订阅不是布尔值。您可以通过向组件类添加变量并使用该变量而不是方法来解决此问题: valid = false; ngOnInit() { this.canEdit(); } canEdit() { let username = this.currentUser.user.username; let values = { "_id"
您在canEdit()方法中返回的订阅不是布尔值。您可以通过向组件类添加变量并使用该变量而不是方法来解决此问题:
valid = false;
ngOnInit() {
this.canEdit();
}
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true }).subscribe(
result => {
if (result.message == "All good!") {
console.log(result);
this.valid = true;
} else {
this.valid = false;
}
}
)
}
valid=false;
恩戈尼尼特(){
这个.canEdit();
}
canEdit(){
让username=this.currentUser.user.username;
设值={
“_id”:this.org._id,
“用户”:用户名
}
this.http.post('http://localhost:3001/users/compareUser,值,{withCredentials:true})。订阅(
结果=>{
如果(result.message==“一切正常!”){
控制台日志(结果);
this.valid=true;
}否则{
this.valid=false;
}
}
)
}
在模板中:
您在canEdit()方法中返回的订阅不是布尔值。您可以通过向组件类添加变量并使用该变量而不是方法来解决此问题:
valid = false;
ngOnInit() {
this.canEdit();
}
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true }).subscribe(
result => {
if (result.message == "All good!") {
console.log(result);
this.valid = true;
} else {
this.valid = false;
}
}
)
}
valid=false;
恩戈尼尼特(){
这个.canEdit();
}
canEdit(){
让username=this.currentUser.user.username;
设值={
“_id”:this.org._id,
“用户”:用户名
}
this.http.post('http://localhost:3001/users/compareUser,值,{withCredentials:true})。订阅(
结果=>{
如果(result.message==“一切正常!”){
控制台日志(结果);
this.valid=true;
}否则{
this.valid=false;
}
}
)
}
在模板中:
另一种方法(*)是返回可观察对象本身并使用|异步
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
//see that return an observable
return this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true })
//we use map to transform the response in a true/false
.pipe(map(result=>{
return result.message=="All good!"?true:false
})
}
canEdit(){
让username=this.currentUser.user.username;
设值={
“_id”:this.org._id,
“用户”:用户名
}
//看到返回一个可观察的
返回此.http.post('http://localhost:3001/users/compareUser,值,{withCredentials:true})
//我们使用map将响应转换为真/假
.pipe(映射(结果=>{
返回结果。消息==“一切正常!”?true:false
})
}
及
...
(*)补充Leo的答案另一种方法(*)是返回可观察对象本身并使用|异步
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
//see that return an observable
return this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true })
//we use map to transform the response in a true/false
.pipe(map(result=>{
return result.message=="All good!"?true:false
})
}
canEdit(){
让username=this.currentUser.user.username;
设值={
“_id”:this.org._id,
“用户”:用户名
}
//看到返回一个可观察的
返回此.http.post('http://localhost:3001/users/compareUser,值,{withCredentials:true})
//我们使用map将响应转换为真/假
.pipe(映射(结果=>{
返回结果。消息==“一切正常!”?true:false
})
}
及
...
(*)补充利奥的回答
canEdit() {
let username = this.currentUser.user.username;
let values = {
"_id": this.org._id,
"user": username
}
//see that return an observable
return this.http.post<any>('http://localhost:3001/users/compareUser', values, { withCredentials: true })
//we use map to transform the response in a true/false
.pipe(map(result=>{
return result.message=="All good!"?true:false
})
}
<div *ngIf="canEdit()|async">
...
</div>