【问题标题】:Angular 2 - Toggle button based on JSON responseAngular 2 - 基于 JSON 响应的切换按钮
【发布时间】:2018-02-15 02:38:25
【问题描述】:

我们如何根据角度 2 中的 JSON 响应将按钮切换为是/否?

【问题讨论】:

  • 通过编写一些代码。如果您甚至不知道从哪里开始,请阅读有关 Angular 的书,或官方文档/教程,然后练习。如果您知道从哪里开始,请展示您尝试过的内容,并解释您面临的具体问题。这太宽泛了。

标签: angular angular2-template angular2-forms angular2-services


【解决方案1】:

使用以下代码切换按钮

import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <button *ngIf='toggleButton' (click)='toggleButton = false'>Button One</button>
      <button *ngIf='!toggleButton' (click)='toggleButton = true'>Button Two</button>
    </div>
  `,
})
export class App {
  name:string;
  toggleButton: boolean: true; // Flag to toggle your button toggling its value on click of button
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

【讨论】:

  • Meta 已经决定我们不应该回答糟糕的问题,尤其是那些希望让 StackOverflow 看起来像代码编写服务的问题。这将有助于鼓励网站上充斥着未来的糟糕问题。
  • 仅供参考,我已经发布了我尝试过的内容。 stackoverflow.com/questions/46065764/…
  • @OptimusPrime 你为什么要发布一个重复的问题呢?如果是后续问题,您为什么不将这个问题链接到您的新问题中?如果这是重复的,则应将其删除。
猜你喜欢
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多