【问题标题】:How do I get different outputs after choosing different radio buttons?选择不同的单选按钮后如何获得不同的输出?
【发布时间】:2019-07-08 19:56:54
【问题描述】:

我正在做一个 Angular 项目。单选按钮应用于创建健身计划。我为 Stackblitz.to 写了一个例子。

https://stackblitz.com/edit/angular-dfcnkc

我有几个数组,我希望根据我的选择给出完美的答案。

homeBreast = ['exercise1', 'exercise2', 'exercise3'];
homeBiceps = ['exercise1', 'exercise2', 'exercise3'];
homeTriceps = ['exercise1', 'exercise2', 'exercise3'];

breast = ['exercise1', 'exercise2', 'exercise3'];
biceps = ['exercise1', 'exercise2', 'exercise3'];
triceps = ['exercise1', 'exercise2', 'exercise3'];

homeTwice1 = [this.homeBiceps, this.homeTriceps]
homeTwice2 = [this.homeBreast]

twice1 = [this.biceps, this.triceps]
twice2 = [this.breast]


homeThrice1 = [this.homeBiceps]
homeThrice2 = [this.homeBreast]
homeThrice3 = [this.homeTriceps]

thrice1 = [this.biceps]
thrice2 = [this.breast]
thrice3 = [this.triceps]

例如:如果我使用“你想多久去一次火车?答案:2”和“我想训练答案:在家”,这应该被删除。

// for the first day in the week
homeTwice1 = [this.homeBiceps, this.homeTriceps]
// for the second day of the week
homeTwice2 = [this.homeBreast]

当我检查答案“3”和“在健身房”时,输出应该是这样的

// for the first day in the week
thrice1 = [this.biceps]
// for the second day of the week
thrice2 = [this.breast]
// for the third day in the week
thrice3 = [this.triceps]

有没有人有提示或想法如何让我做得更好?

【问题讨论】:

    标签: jquery arrays angular typescript output


    【解决方案1】:

    您可以考虑创建一个数组数组(锯齿状数组)来聚合信息。

    例如,您可以将家庭练习的定义放入:

    home = [
      [
        [this.homeBiceps, this.homeTriceps],
        [this.homeBreast]
      ],
      [
        [this.homeBiceps],
        [this.homeBreast],
        [this.homeTriceps]
      ]
    ];
    

    然后,home[0] 将在用户选择两天时保留锻炼计划,home[1] 将定义三天锻炼计划。

    更进一步:

    • home[0][0] 将返回两天中第一天的练习列表:[this.homeBiceps, this.homeTriceps]
    • home[0][1] 将返回第二天和最后一天的练习列表:[this.homeBreast]

    您可以通过将 home 和 gym 组合成一个数组(例如命名为 plans)来进一步扩展数组,但在这种情况下,最好使用常量 HOME=0, GYM=1 来命名数组。

    现在,当您阅读用户的选项时,显示给定的计划会容易得多。比方说,他们选择了三次的频率和健身房的位置,而你显示plans[location][frequency]

    【讨论】:

    • Nojekls 是否可以返回两个数组列表,例如 home[0][0] 和 home[0][1]?
    • 当你返回home[0]时,实际上你返回的是[ [this.homeBiceps, this.homeTriceps], [this.homeBreast] ],它只是一个home[0][0]home[0][1]的数组。
    【解决方案2】:

    创建一个数组并在每个练习中填充它。每个练习都应该是一个包含练习名称和其他属性(如训练频率和训练位置)的对象。这些附加属性将用于动态过滤数组以响应选中的单选按钮。

    [
      {
        name: "exampleExercise1",
        trainingFrequency: [2, 3, 4],
        trainingLocation: ["home"]
      },
      {
        name: "exampleExercise2",
        trainingFrequency: [3, 4],
        trainingLocation: ["home", "gym"]
      },
      ...
    ]
    

    当用户选择 2 和“家”时,根据 trainingFrequency 和 trainingLocation 过滤练习。 "exampleExercise1" 将在上面的示例中返回。

    【讨论】:

      猜你喜欢
      • 2013-07-06
      • 2021-04-25
      • 2017-07-24
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      相关资源
      最近更新 更多