簡體   English   中英

Angular2-雙向數據綁定問題

[英]Angular2 - Two-way data binding issue

我必須單擊一個顯示城市列表的div,當我單擊顯示的城市之一時,我想執行一個方法並顯示cityName,因為到目前為止我的工具是這樣的:

文件1的HTML

 <div [(ngModel)]="cityName" class="heads">{{cityName}}</div>

文件1的.js

@Injectable()
export class queryToDB {

  constructor(http) {
  this.http = http;
  this.valueToBeChanged = "Select a city";
  }

}

文件2

@Component({
      template: `
        <div>
               <button ion-item *ngFor="let city of cities" (click)="switchToThisCity(city.cityName);">{{city.cityName | uppercase}}</button>
        </div>
      `,
      providers:[queryToDB]
    })


export class MyPopover{
static get parameters(){
return [[Http]];
 }


constructor(http) {
    this.http    = http;
 }

 switchToThisCity(currentCity){
  this.cityName = "New York";
  }
 }

但是,我收到以下錯誤:

例外:“”沒有值訪問器

關於導致問題的原因有什么想法?

我認為您不應該嘗試在div上使用ngModel進行兩種方式綁定。

因為ngModel希望在輸入字段或任何其他表單元素上使用它。 這就是為什么您會收到“無值訪問器”錯誤的原因。

你可以在這里找到更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM