【问题标题】:Is there a way to switch between angular material tabs based on a form submission from contents of those tabs?有没有办法根据从这些选项卡的内容提交的表单在角度材料选项卡之间切换?
【发布时间】:2019-09-21 15:13:54
【问题描述】:

我正在开发一个有角度的网络应用程序。我需要根据在这些选项卡的内容中执行的操作来控制我的角度选项卡。例如,我有以下标签: 项目信息, 变化, 价钱 , 库存等 这些选项卡包含由角度组件加载的表单。当项目信息表单成功提交时,我想自动切换到变体选项卡。此外,在保存项目信息之前,用户不应该能够切换到变体选项卡。

<mat-tab-group class=" navbar-default panel-piluku  nav nav-justified nav-wizard nav-progression" > 
  <mat-tab class="outbound_link" role="button" title="Item Info" label="Item Info">
    <add-product-information></add-product-information>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Variations" label="Variations">
    <add-product-variation></add-product-variation>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Pricing" label="Pricing">
      <add-product-pricing></add-product-pricing>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Inventory" label="Inventory">
      <add-product-stock></add-product-stock>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Images" label="Images">
    <add-product-images></add-product-images>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Locations" label="Locations">
    <add-product-locations></add-product-locations>
  </mat-tab>
</mat-tab-group>
constructor(private route: ActivatedRoute, private productsService: ProductsService, private router: Router, public dialog: MatDialog, private suppliersService: SuppliersService) { }
  submitProduct = function(){
    this.product["sku"] = this.sku;
    this.product["alternate_sku"] = this.alternate_sku;
    this.product["product_name"] = this.product_name;
    this.product["product_description"] = this.product_description;
    this.product["category"] = this.category_id;
    this.product["price_per_unit"] = this.price_per_unit;
    this.product["reorder_level"] = this.reorder_level;
    this.product["discontinued"] = this.discontinued;
    this.product["cost"] = this.cost;
    this.product["default_price"] = this.default_price;
    this.product["best_price"] = this.best_price;
    this.product["medium_price"] = this.medium_price;
    this.product["brand"] = this.brand;
    this.product["upc"] = this.upc;
    this.productsService.addProduct(this.product).subscribe((data)=>{
      ````Here the tab should switch```````
    });
<form action="submitProduct()" id="item_form" class="form-horizontal" method="post" accept-charset="utf-8">

  <input type="hidden" name="ecommerce_product_id" value="" />
...
...
...

      <div class="form-actions">
        <input type="submit" name="submitf" value="Save" id="submitf"
          (click)="submitProduct()" class="submit_button floating-button btn btn-lg btn-primary" />
      </div>
    </div>
  </div>
</form>

【问题讨论】:

  • [selectedIndex] 输入 mat-tab-group 。所以你可以改变它来改变标签
  • 是的,但我怎样才能从另一个组件中做到这一点?请注意我的组件已更改:\
  • 很简单,使用事件发射器来通知标签组件的变化
  • 我是 Angular 新手。从来没有这样做过。
  • 您能分享一下您的表单和标签组件到底是如何相关的吗?

标签: angular mat-tab


【解决方案1】:

可以使用selectIndex 输入到mat-tab-group 来控制选择的选项卡。 要更改 tabSelected ,请将其设置为您需要的选项卡索引,然后选项卡将更改为设置的索引。

为此,我只需定义一个服务并使用行为主题。 我的服务定义:

my-tab.service.ts

@Injectable()

export class MyTabService{
 tabSubject : BehaviorSubject<number> = new BehaviorSubject<number>(0);

 constructor(){}

 setTabSelected(tabIndex : number){
  this.tabSubject.next(tabIndex);
 } 

 getTabSelected() {
  this.tabSubject.asObservable();
 }
}

现在在您的标签组件中订阅getTabSelected(),您将在订阅响应中获得tabIndex

考虑到你的标签组件是my-tab.component.ts

@Component({...})
export class MyTabComponent {
 selectdIndex : number = 0;
 constructor(private _myTabService : MyTabService){}

 ngOnInit(){

 this.-myTabService.getTabSelected().subscribe((tabIndex : number) =>{
  this.selectedIndex = tabIndex;
 })

...
 }
...
}

在模板中使用 selectedIndex :

<mat-tab-group class=" navbar-default panel-piluku  nav nav-justified nav-wizard nav-progression" [selectedIndex]='selectedIndex'> 
  <mat-tab class="outbound_link" role="button" title="Item Info" label="Item Info">
    <add-product-information></add-product-information>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Variations" label="Variations">
    <add-product-variation></add-product-variation>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Pricing" label="Pricing">
      <add-product-pricing></add-product-pricing>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Inventory" label="Inventory">
      <add-product-stock></add-product-stock>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Images" label="Images">
    <add-product-images></add-product-images>
  </mat-tab>
  <mat-tab class="outbound_link" role="button" title="Locations" label="Locations">
    <add-product-locations></add-product-locations>
  </mat-tab>
</mat-tab-group>

有关 mat 选项卡组中的输入和事件的更多信息,请参阅:Mat Tab

【讨论】:

  • 标签限制怎么样?
  • @LailaSamaviaKhan 只是禁用标签以限制用户使用 disabled 输入进入其他标签
  • @LailaSamaviaKhan 如果我的回答对您有帮助,请将其标记为答案 :)
猜你喜欢
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 1970-01-01
相关资源
最近更新 更多