如何在角度2中单击更改锚标记文本

时间:2017-07-21 07:39:07

标签: angular

我必须在视图上切换文本并隐藏我该怎么做?我尝试了很多,但没有工作。我不想为此使用Jquery。我想只用角度。谁能帮我这个??

<a  class="history-link view-history-class" id="show-view-address" (click)="view()" >View</a>

组件:

view : boolean ;
    view(){
            this.view = !this.view
        }

为此,当我点击查看它将切换表格。然后文本应更改为隐藏。点击隐藏时也一样。

3 个答案:

答案 0 :(得分:0)

答案最短:

 <a  class="history-link view-history-class" id="show-view-address" (click)="view()">{{view ? 'View'  : 'Hide' }}</a>

答案 1 :(得分:0)

你可以这样做。

text: string = "view";

view(){
    this.text = this.text == "view" ? "hide" : "view" 
}
<a (click)="view()"> {{ text }}</a>

以下是plunker

答案 2 :(得分:0)

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

@Component({
  selector: 'my-app',
  template: `
    <div>
      <a href = "#" (click) = "on()">{{value}}</a>
    </div>
  `,
})
export class App {

  value  = "View";
  toogle = true;

  on(val:boolean){
    //either this
    this.value = (!this.toogle)?"View":"Hide";
    this.toogle = !this.toogle;

    //or this
    this.value = (this.value == "View")?"Hide":"View"; // and remove toogle
  }

}

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

PLUNKER LINK - https://plnkr.co/edit/Wt8s8HSraBsnx9PzgDrA?p=preview