简体   繁体   English

用于将文本添加到 p 标签的角度切换按钮

[英]Angular toggle button to add text to p tag

I have a toggle button , which when clicked should add text 'Dark' to the我有一个切换按钮,单击该按钮时应将文本“暗”添加到

tag.标签。 When clicked again should replace with 'Light'.再次单击时应替换为“Light”。 Initially there shouldn't be anything.Suppose to use `最初不应该有任何东西。假设使用`

    @Component({
      selector: 'darkmode-toggler',
     template: `<div>
                <button>Toggle dark mode</button>
                <p></p>
                </div>`,
                styles: []
                })
         export class DarkModeToggler { }
     @Component({
     selector: 'app-root',
      template: `<div>
     <darkmode-toggler (stateChanged)="handleStateChange($event)"> 
     </darkmode-toggler>
       </div>`,
        styles: []
         })
        export class Resource {  
        handleStateChange(event) {
        console.log(event);  
         }}

I'm not completely sure what you want (a better description would be great), but what I understand from your answer you could do something like this:我不完全确定你想要什么(更好的描述会很棒),但我从你的回答中了解到你可以做这样的事情:

@Component({
  selector: 'app-component',
  template: `
    <button (click)="toggleDarkMode()">Toggle dark mode</button>
    <p>{{isDarkMode ? 'dark' : 'light'}} mode</p>
  `
})
export class AppComponent {
  public isDarkMode = false;

  public toggleDarkMode(): void {
    this.isDarkMode = !this.isDarkMode;
  }
}

or:或者:

type Theme = 'dark' | 'light';

@Component({
  selector: 'app-component',
  template: `
    <button (click)="toggleDarkMode()">Toggle dark mode</button>
    <p>{{theme}} mode</p>
  `
})
export class AppComponent {
  public theme: Theme = 'light';

  public toggleDarkMode(): void {
    this.theme = this.theme === 'light' ? 'dark' : 'light';
  }
}

Edit编辑

In the darkmode-toggler component you can do what was mentioned above to change the paragraph's text.darkmode-toggler组件中,您可以执行上述操作来更改段落的文本。

And then use @Output to give it a custom event.然后使用@Output给它一个自定义事件。

If I edit your sample it would look like this:如果我编辑您的示例,它将如下所示:

@Component({
  selector: 'darkmode-toggler',
  template: `
    <div>
      <button>Toggle dark mode</button>
      <p>{{isDarkMode ? 'dark' : 'light'}} mode</p>
    </div>
  `,
  styles: []
})
export class DarkModeToggler {
  @Output() stateChanged = new EventEmitter<boolean>();
  isDarkMode = false;

  public toggleDarkMode(): void {
    this.isDarkMode = !this.isDarkMode;
    this.stateChanged.emit(this.isDarkMode); // you emit a change here
  }
}

@Component({
  selector: 'app-root',
  template: `
    <div>
      <darkmode-toggler (stateChanged)="handleStateChange($event)"> 
      </darkmode-toggler>
    </div>
  `,
  styles: []
})
export class Resource {  
  handleStateChange(event) {
    console.log(event); // since I made a EventEmitter<boolean>() and I'm emitting the isDarkMode variable's value it'll be true or false
  }
}

添加一个<p>标记到</p><div>在 Angular</div><div id="text_translate"><p> 这是我的 html 代码:</p><pre> &lt;div id="gameboard"&gt; &lt;/div&gt;</pre><p> 这是我在 .ts 文件中的代码</p><pre>document.getElementById('gameboard')?.append('&lt;p&gt;Hello&lt;/p&gt;')</pre><p> 使用此代码,我想将&lt;p&gt;标签添加到我的&lt;div id="gameboard"&gt; 。 单击按钮时,应执行此代码的所有内容。 但不知何故什么也没发生</p></div> - Adding a <p> tag to a <div> in Angular

暂无
暂无

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

相关问题 如何<a>在 angular 7 中切换标签中</a>的文本 - How to toggle the text in <a> tag in angular 7 在Angular中使用ngClass切换按钮的文本 - Toggle text of button with ngClass in Angular Angular 动态翻译切换按钮文本 - Angular translate toggle button text dynamically Angular *ngFor &quot;id&quot; 标签生成不正确,`mat-button-toggle` - Angular *ngFor "id" tag generation incorrect with `mat-button-toggle` Angular,使用 Angular 材料表中的“查看更多”按钮切换文本 - Angular, toggle text with a 'See More' button in Angular material table <a>在中间添加标签</a><p>标签</p> - Add <a> tag in between <p> tag 如何在 Angular 中使用隐藏和显示密码值的按钮切换文本 - How to toggle the text with a button that hides and shows password value in Angular 添加一个<p>标记到</p><div>在 Angular</div><div id="text_translate"><p> 这是我的 html 代码:</p><pre> &lt;div id="gameboard"&gt; &lt;/div&gt;</pre><p> 这是我在 .ts 文件中的代码</p><pre>document.getElementById('gameboard')?.append('&lt;p&gt;Hello&lt;/p&gt;')</pre><p> 使用此代码,我想将&lt;p&gt;标签添加到我的&lt;div id="gameboard"&gt; 。 单击按钮时,应执行此代码的所有内容。 但不知何故什么也没发生</p></div> - Adding a <p> tag to a <div> in Angular 我如何将 div 标签内的按钮居中,以使其响应 Angular 8 中的切换设备工具栏 - How can i center a button inside of a div tag, to make it responsive for toggle device toolbar in Angular 8 使用Angular 4切换按钮的类 - Toggle Class of the button using Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM