简体   繁体   English

Angular 8/9 如何动态禁用同样是动态创建的按钮?

[英]Angular 8/9 How to disable buttons dynamically that were also created dynamically?

So I have a project where I had sets of buttons created dynamically.所以我有一个项目,我有一组动态创建的按钮。 Each set has two buttons, and when one is clicked it's collapsible content appears.每组有两个按钮,当一个按钮被点击时,它的可折叠内容就会出现。 If you click the other button in the set, the first button's content disappears, and the second button's content appears.如果单击集合中的另一个按钮,则第一个按钮的内容消失,而第二个按钮的内容出现。 (For the sake of brevity I figured I'd exclude the content). (为简洁起见,我想我会排除内容)。 The total sets can be determined by simply changing the value of a variable called totalSetsOfButtons.总集合可以通过简单地更改一个名为 totalSetsOfButtons 的变量的值来确定。 Right now, it is set to the number 8, so there will be eight sets of two buttons.现在,它设置为数字 8,因此将有八组两个按钮。

In the first button's collapsible content, there is an image upload that the user can interact with.在第一个按钮的可折叠内容中,有一个用户可以与之交互的图像上传。 If the user interacts with this, I want the second button to disable.如果用户与之交互,我希望禁用第二个按钮。 When the user loads an image, a variable called isImageAdded is switched from null to true , which tells the disabled attribute to trigger in the second button as you can see below.当用户加载图像时,名为isImageAdded的变量从null切换到true ,这告诉disabled属性在第二个按钮中触发,如下所示。 The problem is, if an image is added, this disables ALL of the second buttons in every set.问题是,如果添加图像,这会禁用每组中的所有第二个按钮。

<div *ngFor="let item of totalSetsOfButtons; let i = index">
  <h5 class="set-num">Set #{{i+1}}</h5>

  <div class="set-btns">
    <div class="set{{i}}-btn1" id="set-btn1">
      <p>
        <button class="btn btn-primary btn-light question-btn1" type="button" data-toggle="collapse" [attr.data-target]="'#collapseExample' + i" aria-expanded="false" [attr.aria-controls]="collapseExample1"> Add New Question
        </button>
      </p>
    </div>

    <div class="set{{i}}-btn2" id="set-btn2">
      <p>
        <button class="btn btn-primary btn-light question{{i}}-btn2" [disabled]="isImageAdded" type="button" data-toggle="collapse" [attr.data-target]="'#collapseExample2' + i" aria-expanded="false"
                        [attr.aria-controls]="collapseExample2">Add Pre-existing Question
        </button>
      </p>
    </div>
  </div>
</div>

How do I only disable the second button in the set the user is interacting with?如何仅禁用用户正在与之交互的集合中的第二个按钮? Any help is appreciated!任何帮助表示赞赏!

The problem is that you have set the [disabled] property to isImageAdded which is a global property in the component.问题是您已将 [disabled] 属性设置为isImageAdded ,它是组件中的全局属性。 So setting it to true/false will affect the disabled state of all the buttons bound to it.因此将其设置为 true/false 将影响绑定到它的所有按钮的禁用 state。

In it's place, you might prefer to set a variable in the totalSetsOfButtons array, and use that to enable/disable the corresponding button.在它的位置,您可能更喜欢在totalSetsOfButtons数组中设置一个变量,并使用它来启用/禁用相应的按钮。

For example:例如:

totalSetsOfButtons = [
  { ...yourEarlierArgs, isImageAdded: false },
  { ...yourEarlierArgs, isImageAdded: false },
  ... more 6 similar items
];

And when you interact with the application, you can update the value of the isImageAdded in the array item, and use it.并且在与应用程序交互时,可以更新数组项中 isImageAdded 的值,并使用它。

<button [disabled]="item.isImageAdded">Add Pre-existing Question</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM