简体   繁体   English

Angular2-双向数据绑定问题

[英]Angular2 - Two-way data binding issue

I have to click on a div that show a list of cities, when I click on one of the cities displayed I want to execute a method and display the cityName, for that my implement is so far like this: 我必须单击一个显示城市列表的div,当我单击显示的城市之一时,我想执行一个方法并显示cityName,因为到目前为止我的工具是这样的:

Html of file 1 文件1的HTML

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

.js of file 1 文件1的.js

@Injectable()
export class queryToDB {

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

}

File 2 : 文件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";
  }
 }

However, I receive the below error: 但是,我收到以下错误:

EXCEPTION: No value accessor for '' 例外:“”没有值访问器

Any ideas on what's causing the issue? 关于导致问题的原因有什么想法?

I think you shouldn't be tried to use ngModel to two way bind on div. 我认为您不应该尝试在div上使用ngModel进行两种方式绑定。

Because ngModel expects to use it on an input field or any other form elements. 因为ngModel希望在输入字段或任何其他表单元素上使用它。 That's why you get the No value accessor error. 这就是为什么您会收到“无值访问器”错误的原因。

You can find out more at here 你可以在这里找到更多

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

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