2

我有两个要根据*ngIf条件值隐藏和显示的元素。

默认情况下,我的wrap块是可见的,当我单击其中一个块时,divswrap块应该消失,并且只有选定的div's内容应该显示在我的span元素中。然后,当我单击我的span元素时,它应该会消失,并且wrap块应该以其所有默认内容再次出现。

为了实现这一点,我对这两个元素都使用*ngIf了 value visability,期望我的函数以下列方式工作:

wrap - visible;
span - hidden;

wrap - hidden;
span - visible;

这是我的代码:

@Component({
selector: 'my-app',
template: `
<div id="wrap" class="wrap" *ngIf="visability">
<div (click)="clicked($event)">
  <h2>Hello {{name}}</h2>
</div>
<div (click)="clicked($event)">
  <h2>Hello {{year}}</h2>
</div>
<div (click)="clicked($event)">
  <h2>Hello {{surname}}</h2>
</div>
<div (click)="clicked($event)">
  <h2>Hello {{country}}</h2>
</div>
<div (click)="clicked($event)">
  <h2>Hello {{cartoon}}</h2>
</div>

 <div class="view">
  <span id="span" (click)="showDivs()" *ngIf="visability">{{target}}    </span>
 </div>

 `,
})
export class App {
name:string;
year: string;
surname: string;
country: string;
cartoon: string;
element: any;
target: any;

constructor() {
var wrap = document.getElementById("wrap");
var span = document.getElementById("span");
this.name = 'Angular2'
 this.year = '1989'
  this.surname = 'Connor'
   this.country = 'USA'
    this.cartoon = 'Tom & Jerry'
  }

clicked(event) {
wrap.visability = false;
span.visability = true;
this.target = event.target.innerText;
} 

showDivs(){
span.visability = false;
wrap.visability = true;
}

}

我的笨蛋

为什么我的功能无法正常工作?

4

2 回答 2

2

我实际上改变了你的代码。第一个div 设置visability为 true,第二个设置为 false。更好地把它们分开。然后它只是根据点击切换布尔值,如下所示:

首先将可见性声明为变量,以便您可以引用它,this然后只需切换可见性。

  clicked(event) {
    this.visability = false;
    this.target = event.target.innerText;
  }

  showDivs(){
    this.visability = true;
    // span.visability = false; // remove this
    // wrap.visability = true;  // remove this
  }

和相应的html:

<div id="wrap" class="wrap" *ngIf="visability">

<span id="span" (click)="showDivs()" *ngIf="!visability">{{target}}</span>

这是一种解决方案。

你的笨蛋

于 2017-01-20T21:14:47.670 回答
0

我通过删除*ngIf我的第二个元素来解决它。我刚刚使用 *ngIf="shown" 来表示span.

然后在构造函数中我同时设置

visibility = true

shown = true

在函数内部,我将值切换为相反,从而设法实现了我想要的,尽管我不知道这是否是最好的方法。

@Component({
  selector: 'my-app',
  template: `
  <div id="wrap" class="wrap" *ngIf="visibility">
    <div (click)="clicked($event)">
      <h2>Hello {{name}}</h2>
    </div>
    <div (click)="clicked($event)">
      <h2>Hello {{year}}</h2>
    </div>
    <div (click)="clicked($event)">
      <h2>Hello {{surname}}</h2>
    </div>
    <div (click)="clicked($event)">
      <h2>Hello {{country}}</h2>
    </div>
    <div (click)="clicked($event)">
      <h2>Hello {{cartoon}}</h2>
    </div>
  </div>

  <div class="view">
    <span id="span" (click)="showDivs()" *ngIf="shown">{{target}}</span>
  </div>

  `,
})
export class App {
  name:string;
  year: string;
  surname: string;
  country: string;
  cartoon: string;
  element: any;
  target: any;

  clicked(event) {
     this.target = event.target.innerText;
     this.visibility = false;
     this.shown = true;
  }

  showDivs(){
     this.shown = false;
     this.visibility = true;
  }

  constructor() {
    this.visibility = true;
    this.shown = true;
    this.name = 'Angular2'
     this.year = '1989'
      this.surname = 'Connor'
       this.country = 'USA'
        this.cartoon = 'Tom & Jerry'
  }



}

工作小插曲

于 2017-01-20T21:18:18.400 回答