蔭新的角2,發佈之前,我試圖尋找在互聯網上一個解決方案,但我不能讓solution.i應該心存感激,如果有人可以幫助我角2組件與ngModel不工作
我的代碼下面的工作沒有ngModel
user.component
import { Component } from '@angular/core';
@Component({
selector: 'user',
template: `<h3>Welcome {{name}}</h3>
<p>EMAIL: {{email}}</p>
<div *ngIf="showHobbies">
<p>HOBBIES:</p>
<ul>
<li *ngFor="let hobby of hobbies">
{{hobby}}
</li>
</ul>
</div>
`,
})
export class UsersComponent {
username:string;
private name:string;
private email:string;
private hobbies:string[];
private showHobbies:boolean;
constructor(){
this.username="";
this.showHobbies=false;
this.name="My name";
this.email="My Email";
this.hobbies=['swimming','watching movie','playing football'];
}
toggleHobbies(){
if(this.showHobbies==true){
this.showHobbies = false;
}
else {
this.showHobbies = true;
}
}
}
app.module這個樣子
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { UsersComponent } from './components/users.component';
@NgModule({
imports: [ BrowserModule,FormsModule],
declarations: [ AppComponent,UsersComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
我app.component這個樣子的
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<user></user>`,
})
export class AppComponent { }
上面的代碼停止時,我加入形式ngModule爲2路在user.component結合工作
@Component({
selector: 'user',
template: `<h3>Welcome {{name}}</h3>
<p>EMAIL: {{email}}</p>
<p>TEL: {{telephone}}</p>
<button (click)="toggleHobbies()">{{ showHobbies ? 'Hide hobbies' : 'Show hobbies'}}</button>
<div *ngIf="showHobbies">
<p>HOBBIES:</p>
<ul>
<li *ngFor="let hobby of hobbies">
{{hobby}}
</li>
</ul>
</div>
當我在下方加入其他代碼停止形式到作品
<form>
<label>Name</label> <br/>
<input type="text" [(ngModel)]="username" />
<p>Hello {{username}} </p>
</form>
`,
})
你有什麼錯誤訊息?你有沒有試過這個: http://stackoverflow.com/questions/38365761/angular2-use-ngmodel-with-ngmodeloptions-standalone-true-to-link-to-a? – Nikolai