【问题标题】:ngIf my function errorngIf 我的函数错误
【发布时间】:2018-04-30 19:02:53
【问题描述】:

我正在尝试显示数据库中的数据。然后我想验证每个数据是否存在于另一个表中,所以我需要向我的后台发送请求以检查这些数据。然后我需要显示它是否有效。

问题是*ngIf 不起作用,它表明我的提供程序功能不起作用。

这是 HTML 页面:

<ion-row *ngFor="let obj of listObj">
    <ion-col col-8>
        <ion-icon name="md-checkmark-circle-outline"></ion-icon>
        <p>Objectif : {{obj.id_obj}} </p>
        <p id="nameobj"> {{obj.des_obj | titlecase}}</p>
    </ion-col>
    <ion-col col-4 *ngIf="testobj(obj.id_obj_ser).length">
        <p>lol</p>
        <!--
        <p id="valide">Vue</p>
        <p id="non-valide">Non Vue</p>-->
    </ion-col>
</ion-row>

这是我的控制器:

export class ObjectifEnsPage {
    listObj = <[any]>[];
    testiObj = <[any]>[];
    id_stage: number;
    cin: number;
    test: boolean;

    constructor(public navCtrl: NavController, public navParams: NavParams, private etudiant: EtudiantProvider) {
        this.id_stage = this.navParams.get('stage');
        this.cin = this.navParams.get('cin');
        console.log("el stage w el cin ahawma" + this.id_stage, this.cin);
    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad ObjectifEnsPage');
        this.getobjectif()
    }

    getobjectif() {
        this.etudiant.getobjectif(this.id_stage).subscribe((data: any) => {
            this.listObj = data;
            console.log(data);
        }, error => {
            console.log(error);
        });
    }

    testobj(id: number): any {
         this.etudiant.getobjectifvalidation(id, this.cin).subscribe((data: any) => {
             this.testiObj = data;
         }, error => {
             console.log(error);
         });
    }
}

【问题讨论】:

  • 请用您遇到的确切错误更新您的问题。
  • 但是你没有返回一个布尔值,如果你会从方法中返回一个真值。毕竟这是一个 If 条件。
  • @DanielWSrimpel 在 testobject 函数上进入无限循环
  • 我认为您的流程完全颠倒了。您可能正在经历这个“无限循环”,因为每次 Angular 启动更改检测周期时,您的 testobj 函数都会在该 *ngIf 中运行。您正在加载数据并将其设置在您的对象上(通过this.testiObj 属性),这将触发另一个更改检测周期......从而调用您的testobj 函数。您能否提供控制器代码,以便我们尝试帮助您朝另一个方向发展?
  • 就是这样,我想做的是验证收到的数据是否不为空,然后 ngif 工作。 @DanielWSrimpel

标签: angular http ionic-framework angular-ng-if


【解决方案1】:

如果您使用的是*ngIf,则应返回boolean 你的函数要么返回一个布尔值,要么返回一个解析为布尔值的 Observable

private testiObj$: Observable<any>;

testobj(id: number): any {
     this.testiObj$ = this.etudiant.getobjectifvalidation(id, this.cin);

现在您可以在模板中使用 async pipe 订阅此内容

<ion-col col-4 *ngIf="testobj(obj.id_obj_ser) | async">

只要 Observable 返回一个布尔值。 延伸阅读:

https://toddmotto.com/angular-ngif-async-pipe

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 1970-01-01
    • 2020-11-06
    • 2019-12-05
    • 2018-03-25
    • 2021-12-19
    • 1970-01-01
    • 2017-10-06
    • 2017-09-21
    相关资源
    最近更新 更多