简体   繁体   English

从html到ts的值

[英]value from html to ts

I'm new to Angular and need help please. 我是Angular的新手,请帮助。

I have two components..one for index and one for Navigation. 我有两个组件..一个用于索引,一个用于导航。

for the index component there are subcomponents, which is changed by a value of a variable productDivision..so if productDivision = 1, the sub-component (A) will be shown in the index and if the productDivision = 2, the subcomponent 2 will be shown instead of 1. 对于索引组件,存在子组件,这些子组件会通过变量productDivision的值进行更改。因此,如果productDivision = 1,则子组件(A)将显示在索引中,如果productDivision = 2,则子组件2将显示在索引中。显示而不是1。

so what I need is ... if I click an element on the navbar, i want the value of the product division to change..to display the different sub-component in the index here is my code. 因此,我需要的是...如果单击导航栏上的一个元素,我希望更改产品部门的值..以便在索引中显示不同的子组件,这是我的代码。 so how can I take the new value of productDivision variable from HTML and assign it to ts as a new value of the variable 所以我怎么能从HTML中获取productDivision变量的新值并将其分配给ts作为变量的新值

navbar component: html: 导航栏组件:html:

<nav id="nav-icons">
    <a class="nav-icon-home" (click)="prodProvision === null; detectProductDivision() " routerLink="/">
      <span class="nav-icon"></span><br />
      Telefon
    </a>
    <a class="nav-icon-mobile" (click)="prodProvision === 1 ;detectProductDivision()" routerLink="/" >
      <span class="nav-icon"></span><br />
      Mobil
    </a>

    <a class="nav-icon-phone" (click)="prodProvision === 2; detectProductDivision()" routerLink="/">
      <span class="nav-icon"></span><br />
      Fax
    </a>

navbar.ts navbar.ts

  import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';

    @Component({
      selector: 'app-navbar',
      templateUrl: './navbar.component.html',
      styleUrls: ['./navbar.component.css']
    })
    export class NavbarComponent implements OnInit {
       public productDivision: number;
         public prodProvision;
        @Output() public navbarEmitter = new EventEmitter();
      constructor() { }
        detectProductDivision() {
            this.navbarEmitter.emit(this.prodProvision);
        }

      ngOnInit() {
      }
    }
    }

I do not know if I understood correctly, but you could pass the value through the click function: 我不知道我是否正确理解,但是可以通过click函数传递值:

(click)="myClickFunction(valueThatIwantToPass)"

Please if this does not solve your question, expand your question so I can provide you with a better answer, thanks! 如果这不能解决您的问题,请扩展您的问题,以便为您提供更好的答案,谢谢!

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

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