Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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 离子选择不';使用过滤数据时,不呈现选定值_Javascript_Angular_Ionic2 - Fatal编程技术网

Javascript 离子选择不';使用过滤数据时,不呈现选定值

Javascript 离子选择不';使用过滤数据时,不呈现选定值,javascript,angular,ionic2,Javascript,Angular,Ionic2,我遇到了离子选择动态数据过滤的奇怪错误。在我的应用程序中,用户在注册时应选择三个安全问题,因此这些问题不能相同。我有一系列问题: questions: Array<{isSelected: boolean;question: string}> = [ {isSelected: false, question: 'What is your pet’s name?'}, {isSelected: false, question: 'What is the first n

我遇到了离子选择动态数据过滤的奇怪错误。在我的应用程序中,用户在注册时应选择三个安全问题,因此这些问题不能相同。我有一系列问题:

 questions: Array<{isSelected: boolean;question: string}> = [
    {isSelected: false, question: 'What is your pet’s name?'},
    {isSelected: false, question: 'What is the first name of the person you first kissed?'},
    {isSelected: false, question: 'Who was your childhood hero?'},
    {isSelected: false, question: 'What was your first grade friend last name?'},
    {isSelected: false, question: 'Where did you celebrate your 20th birthday?'},
    {isSelected: false, question: 'In what city or town does your nearest sibling live?'},
    {isSelected: false, question: 'What time of the day were you born?'}];
根据日志,逻辑工作正常。这是我第一次使用问题过滤为离子选择提供问题,遇到问题:所选选项不显示在UI字段中。然后我尝试使用管道作为建议的方式过滤数据,效果相同。 我将感谢你的建议。多谢各位

更新

为了澄清这个问题,我添加了一些记录,并重写了一些代码。本例中三个问题中有两个有管道,最后一个没有。我得到了正确的值,但没有实际显示

更新


我仍然不知道为什么@misha130提出的解决方案没有给出任何结果。所以我试着用Chrome inspector四处搜索,发现这个部分没有被调用。(我复制了突出显示的部分,但我不确定它是否正确)

Angular2没有检测到数组及其子对象中的更改。要使它在您的视图中真正生效,您必须调用更改检测

import { ChangeDetectorRef } from '@angular/core';

// Inject to constructor
constructor(public cd:ChangeDetectorRef){}

// as per your example call it on in your ionChange method
chooseQuestion(s: string) {
  this.cd.detectChanges();
}

有关更多信息:

什么是UI字段?离子选择它自己?我想这是因为您在一个数组中,而更改检测不适用于数组,所以您必须自己应用检测更改。也可以使用(ionChange)=“chooseQuestion($event)”来代替您的身份using@misha130您好,我已经更改为使用事件,谢谢。请澄清我如何应用检测更改?我还更新了我的问题,演示了我的问题。很好的文章,谢谢。但不幸的是,它没有起作用。
 chooseQuestion(s: string) {
    console.log(this.TAG + 'chooseQuestion: event value: ' + s);
    this.questions.filter(res => res.question == s).map(res => res.isSelected = true);
    //todo rewrite this 
    this.questions.filter(res => res.isSelected == true &&
    res.question != this.regModel.SecurityQuestions[0].Question &&
    res.question != this.regModel.SecurityQuestions[1].Question &&
    res.question != this.regModel.SecurityQuestions[2].Question)
      .map(res => res.isSelected = false);
    this.questions_filtered = this.questions.filter(res => res.isSelected == false);

    console.log(this.TAG + 'chooseQuestion: questions: ' + JSON.stringify(this.questions));
    console.log(this.TAG + 'chooseQuestion: questions_filtered: ' + JSON.stringify(this.questions_filtered));
  }
import { ChangeDetectorRef } from '@angular/core';

// Inject to constructor
constructor(public cd:ChangeDetectorRef){}

// as per your example call it on in your ionChange method
chooseQuestion(s: string) {
  this.cd.detectChanges();
}