如何在Angular和Typescript中从标签数组中禁用标签元素

huangapple go评论96阅读模式
英文:

How to disable a tab element from array of tabs in Angular and Typescript

问题

以下是用户可以单击的5个标签数组。我想禁用标签元素2和3。因此,标签名称可见,但用户不应能够单击这些标签。
我尝试在typescript文件中将标签设置为active: false,但没有成功。我不确定该怎么做。
非常感谢任何帮助或建议。

TypeScript

public static User_Tabs: any[] = [
  { value: 'user_tab_one', viewValue: '用户1' },
  { value: 'user_tab_two', viewValue: '用户2' },
  { value: 'user_tab_three', viewValue: '用户3' },
  { value: 'user_tab_four', viewValue: '用户4' },
  { value: 'user_tab_five', viewValue: '用户5' }
];

HTML

<div class="user-tabs">
  <ng-container *ngFor="let tab of userTabs">
    <div class="filter" (click)="onTabSelect(tab.value)" [ngClass]="{'activeTab': tab.value === userTabType}">
      {{ tab.viewValue }}
    </div>
  </ng-container>
</div>
英文:

Below is the 5 array of tabs that the user can click. I want to disable the tabs element 2 and 3. So that the tab name is visible but the user should not be able to click on those tab.
I have tried setting the tabs to active: false in the typescript file but that didn't work. I'm not sure on how to go about it.
Any help or suggestions would be greatly appreciated.

TypeScript

public static User_Tabs: any [
{ value: &#39;user_tab_one&#39;, viewValue: &#39;User 1&#39;}, 
{ value: &#39;user_tab_two&#39;, viewValue: &#39;User 2&#39;}, 
{ value: &#39;user_tab_three&#39;, viewValue: &#39;User 3&#39;}, 
{ value: &#39;user_tab_four&#39;, viewValue: &#39;User 4&#39;},
{ value: &#39;user_tab_five&#39;, viewValue: &#39;User 5&#39;}];

HTML

&lt;div class= &quot;user-tabs&quot;&gt;
&lt;ng-container *ngFor = &quot;let tab of userTabs&quot;&gt;
&lt;div class=&quot;filter&quot; (click)=&quot;onTabSelect(tab.value)&quot; 
[ng-class]=&quot;{&#39;activeTab&#39;: tab.value === userTabType}&quot;&gt;
{{tab.viewValue}}&lt;/div&gt;
&lt;/ng-container&gt;
&lt;/div&gt;

</details>


# 答案1
**得分**: 1

你可以在你的 User_Tabs 数组中添加一个 isActive 标志,对于元素 2 和 3,将 isActive 的布尔值设为 false。

```typescript
public static User_Tabs: any[] = [
  { value: 'user_tab_one', viewValue: 'User 1', isActive: true },
  { value: 'user_tab_two', viewValue: 'User 2', isActive: false },
  { value: 'user_tab_three', viewValue: 'User 3', isActive: false },
  { value: 'user_tab_four', viewValue: 'User 4', isActive: true },
  { value: 'user_tab_five', viewValue: 'User 5', isActive: true }
];

在 HTML 中,你可以使用 [disabled] 属性。

<ng-container *ngFor="let tab of userTabs">
  <div class="filter" (click)="onTabSelect(tab.value)" [disabled]="!tab.isActive">
    {{ tab.viewValue }}
  </div>
</ng-container>
英文:

You can add isActive flag in your User_Tabs array & for element 2 and 3 pass isActive Boolean value as false

public static User_Tabs: any [
  { value: &#39;user_tab_one&#39;, viewValue: &#39;User 1&#39;, isActive :true}, 
  { value: &#39;user_tab_two&#39;, viewValue: &#39;User 2&#39;, isActive :false}, 
  { value: &#39;user_tab_three&#39;, viewValue: &#39;User 3&#39;, isActive :false}, 
  { value: &#39;user_tab_four&#39;, viewValue: &#39;User 4&#39;, isActive :true},
  { value: &#39;user_tab_five&#39;, viewValue: &#39;User 5&#39;, isActive :true}
];

In Html, you can use [disabled] attribute.

&lt;ng-container *ngFor = &quot;let tab of userTabs&quot;&gt;
  &lt;div class=&quot;filter&quot; (click)=&quot;onTabSelect(tab.value)&quot; [disabled]=&#39;!tab.isActive&#39;&gt;
  {{tab.viewValue}}&lt;/div&gt;
&lt;/ng-container&gt;

huangapple
  • 本文由 发表于 2020年1月3日 13:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定