简体   繁体   English

angular4中的双向绑定问题

[英]Two way binding issue in angular4

I am not able to get the two way binding in angular4 working. 我无法在angular4工作中获得双向绑定。 Here is the component code. 这是组件代码。

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-date-selector',
  templateUrl: './date-selector.component.html',
  styleUrls: ['./date-selector.component.css']
})
export class DateSelectorComponent implements OnInit {

  private statDate: Date;

  constructor() { }

  ngOnInit() {
    this.statDate = new Date()
  }

  goToPreviousDay(): void
  {
    console.log("previous day clicked");
    this.statDate.setDate(this.statDate.getDate() - 1);
    console.log(this.statDate);
  }

  goToNextDay(): void
  {
    console.log("next day clicked");
    this.statDate.setDate(this.statDate.getDate() + 1);
    console.log(this.statDate);
  }
}

I am referring to statDate in my partial like this. 我在这样的部分中指的是statDate。

<div>
    <a><span class="glyphicon glyphicon-chevron-left" (click)=goToPreviousDay()></span></a>
    <span class="text-center text-muted" [(innerText)]="statDate"></span>
    <a><span class="glyphicon glyphicon-chevron-right" (click)=goToNextDay()></span></a>
</div>

Console log shows that the statDate is getting updated, but the same is not getting reflected in the UI. 控制台日志显示statDate正在更新,但未在UI中反映出来。

Several issues to be pointed out. 需要指出的几个问题。

Angular Binding 角度绑定

In your case, since we're talking about a span, all you need is one-way binding. 在您的情况下,由于我们正在讨论跨度,因此您只需要单向绑定即可。 There are several ways to do that in Angular. 在Angular中有几种方法可以做到这一点。

The most common, readable and simple way to do this would be with an interpolation . 最常见,易读和简单的方法是使用插值 Also you are trying to display a date, so you should use Angular's DatePipe like this: 另外,您正在尝试显示日期,因此应使用Angular的DatePipe如下所示:

<span>{{statDate | date}}</span>

That will pretty print your date with several variables to format it like you want. 这将漂亮地打印您的日期,并带有多个变量以按您希望的方式格式化它。


HTML event binding syntax HTML事件绑定语法

Also your click event binding in HTML should look like this: 此外,HTML中的点击事件绑定应如下所示:

<span (click)="goToPreviousDay()"></span>

Note the " after the (click)= , it is pretty common for the Angular code to stop its execution on HTML/TS syntax typos and that would explain the lack of UI update. 注意(click)=之后的" ,Angular代码停止在HTML / TS语法拼写上停止执行是很常见的,这可以解释缺少UI更新的原因。


Result 结果

Combining all that is mentioned above, the resulting HTML would be: 结合上面提到的所有内容,结果HTML将是:

<div>
  <a>
    <span class="glyphicon glyphicon-chevron-left" (click)="goToPreviousDay()"></span>
  </a>
  <span class="text-center text-muted">{{statDate | date}}</span>
  <a>
    <span class="glyphicon glyphicon-chevron-right" (click)="goToNextDay()"></span>
  </a>
</div>

Instead of two-way binding , all you need here is one-way binding . 除了two-way binding ,您所需要做的只是one-way binding

Change this: 更改此:

<span class="text-center text-muted" [(innerText)]="statDate"></span>

to : 至 :

<span class="text-center text-muted" > {{ statDate }}</span>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM