简体   繁体   English

Angular - 调整大小更改 html 元素

[英]Angular - On resize change html element

I am using Angular 5. What I am trying to do is a responsive side navigation bar.我正在使用 Angular 5。我想要做的是一个响应式侧导航栏。 When the window width changes I need some events to apply (css, content etc).当窗口宽度发生变化时,我需要应用一些事件(css、内容等)。 In my example I want to change the inner Html - text, when the window width is below of 1080 pixels.在我的示例中,当窗口宽度低于 1080 像素时,我想更改内部 Html - 文本。

I tried three ways.我尝试了三种方法。 By javascript and ng-model but nothing.通过 javascript 和 ng-model 但什么都没有。

Html:网址:

<div id="nav-left">
    <mat-list>
     <mat-list-item><a id="elp1" [routerLink]="['/']" fragment="intro-text" ng-model="innerHtml"> Welcome </a></mat-list-item>
     <mat-list-item><a id="elp2" [routerLink]="['/']" fragment="introduction"> Introduction </a></mat-list-item>
     ...
    </mat-list>
</div> 

Component (first way):组件(第一种方式):

@HostListener('window:resize', ['$event'])
    onResize(event?) {      
      this.screenWidth = window.innerWidth;
      let bgScreen = true;
      let smScreen = true;
      if(this.screenWidth < 1080 && smScreen){
        document.getElementById("elp1").innerHTML = "New";

        smScreen = false; 
        bgScreen = true;

      }else if(this.screenWidth >= 1080 && bgScreen){
        document.getElementById("elp1").innerHTML = " Welcome ";

        bgScreen = false;
        smScreen = true;        
      }else{
        console.log('problem');
      }
    }

In this case I get a console error:在这种情况下,我收到控制台错误:

Uncaught (in promise): TypeError: Cannot set property 'innerHTML' of null

Component (second way):组件(第二种方式):

@HostListener('window:resize', ['$event'])
        onResize(event?) {      
          this.screenWidth = window.innerWidth;

          if(this.screenWidth < 1080){
            let element = <HTMLInputElement>document.getElementById("elp1");
            element.value = "New";

          }else if(this.screenWidth >= 1080){
            let element = <HTMLInputElement>document.getElementById("elp1");
            element.value = "Welcome";      
          }else{
            console.log('problem');
          }
        }

With the error:随着错误:

Uncaught (in promise): TypeError: Cannot set property 'value' of null

Component (third way & ng-model):组件(第三种方式和 ng-model):

@HostListener('window:resize', ['$event'])
            onResize(event?) {      
              this.screenWidth = window.innerWidth;

              if(this.screenWidth < 1080){
                let innerHtml :string = "New";

              }else if(this.screenWidth >= 1080){
                let innerHtml :string = "Welcome";      
              }else{
                console.log('problem');
              }
            }

With no errors but it doesn't work.没有错误,但它不起作用。

1) The first problem are the above errors. 1)第一个问题是上面的错误。 2) and secondly as you see in the "first way" example I tried to add flags trigger effects and realized that didn't worked either. 2)其次,正如您在“第一种方式”示例中看到的那样,我尝试添加标志触发效果并意识到这也不起作用。 Variables "bgScreen" and "smScreen" are always true.变量“bgScreen”和“smScreen”始终为真。 I put those for a better coding flow.我把它们放在一个更好的编码流程中。

I don't want to use jQuery.我不想使用 jQuery。 Only typescript or with angular (ng-model) way.只有打字稿或带有角度(ng-model)的方式。 Any suggestions?有什么建议?

I recommend using data binding to set the content of the link:我建议使用数据绑定来设置链接的内容:

<div id="nav-left">
  <mat-list>
    <mat-list-item><a [routerLink]="['/']" fragment="intro-text"> {{introText}} </a></mat-list-item>
     ...
  </mat-list>
</div>

The public property introText can be modified when the window is resized:调整窗口大小时可以修改公共属性introText

introText = "Welcome";

@HostListener("window:resize", ["$event"])
onResize(event) {      
  this.introText = window.innerWidth < 1080 ? "New" : "Welcome";
}

or, as an alternative, you could define introText as a getter (not handling the resize event):或者,作为替代方案,您可以将introText定义为 getter(不处理resize事件):

get introText(): string {
  return window.innerWidth < 1080 ? "New" : "Welcome";
}

To get element instead of using document.getElementById("elp1") try ViewChild of angular if child element of component, or ElementRef for component itself.要获取元素而不是使用document.getElementById("elp1")尝试ViewChild angular 如果组件的子元素的ElementRef ,或组件本身的ElementRef As here it looks child element of Component you could use ViewChild在这里它看起来像 Component 的子元素,你可以使用ViewChild

You will get the access to element in angular component.您将可以访问角度组件中的元素。

so now your HTML would be所以现在你的 HTML 将是

<mat-list-item><a #elp1 [routerLink]="['/']" fragment="intro-text"> Welcome </a></mat-list-item>

and in @component并在@component

import { ViewChild} from '@angular/core';
.
.
@ViewChild('elp1') link;

//And assign values like below now.
this.link.nativeElement.innerHTML = "New";

Hope this helps.希望这可以帮助。

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

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