Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript *ngIf会创建一个无限循环,并且永远不会解析_Javascript_Angular_Typescript_Express - Fatal编程技术网

Javascript *ngIf会创建一个无限循环,并且永远不会解析

Javascript *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"

我在角度上工作,只想在某些值对齐时显示某种形式。我有后端路线的工作和测试与邮递员。这是我的密码

大宗报价 角度组件HTML


您在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>