代码之家  ›  专栏  ›  技术社区  ›  Ibrahim Azhar Armar

angular-更新根index.html中的html属性

  •  0
  • Ibrahim Azhar Armar  · 技术社区  · 6 年前

    我的index.html中有以下内容

    <!doctype html>
    <html dir="ltr" lang="en">
        <head>
            <meta charset="utf-8">
            <title>Halls Gate</title>
            <base href="/">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="icon" type="image/png" href="assets/img/favicon.png">
            <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700" rel="stylesheet">
            <link href="https://fonts.googleapis.com/css?family=Istok+Web" rel="stylesheet">
        </head>
        <body>
            <app-root></app-root>
        </body>
    </html>
    

    我想从组件更新html元素中的dir和lang属性 <html dir="ltr" lang="en"> 基于所选语言。

    有没有办法从角度来讨论这个问题?

    谢谢您。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Serkan KONAKCI    6 年前

    我想,它可以简单地改变…

    https://stackblitz.com/edit/angular-ih2drk?file=src%2Fapp%2Fapp.component.ts

    import { Component, Renderer2 } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      name = 'Angular 6';
      constructor(private renderer: Renderer2) {
        this.renderer.setAttribute(document.querySelector('html'), 'lang', 'tr');
      }
    }
    
    
        2
  •  1
  •   westdabestdb    6 年前

    这将是一个糟糕的做法。你应该使用 Internationalization 相反。 国际化是设计和准备应用程序以便在不同语言中使用的过程。本地化是将国际化应用程序翻译成特定语言的过程。

    从以下几点开始:

    ng serve --configuration=your locale id
    
    推荐文章