0

I'm running the Angular tutorial. I'm stuck.

I did a search on here, I found a similar error , but it doesn't help me.

The error I'm getting:

Error: Uncaught (in promise): Error: Template parse errors:
Parser Error: The '?.' operator cannot be used in the assignment at column 20 in [selectedHero?.name=$event] in ng:///AppModule/AppComponent.html@15:19 ("
          <div>
            <label>name: </label>
            <input [ERROR ->][(ngModel)]="selectedHero?.name" placeholder="name"/>
          </div>
        </div>
"): ng:///AppModule/AppComponent.html@15:19 

I tried with and without the ?. Doesn't make difference. I tried the line:

<input [ngModel]="selectedHero?.name" (ngModelChange)="selectedHero.name = $event" />

I went back to the page and copied the entire code, it doesn't change anything.

I got the same error on the very first part of the tutorial too.

user269964
  • 159
  • 11
  • 2
    You can't use the `?` in two-way binding. Your second example is correct. `(ngModelChange)="selectedHero?.name ? selectedHero.name = $event : null">` is even better. What happens with the second approach? – eko Jun 15 '17 at 15:55
  • I put this in : , and I get nothing. I just have an edit box, but nothing it is in it. – user269964 Jun 15 '17 at 16:58
  • You forgot `[(ngModel)]="selectedHero?.name"` – eko Jun 15 '17 at 17:23
  • Actually, I tried that too. Didn't work. Unhandled Promise rejection: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'. – user269964 Jun 15 '17 at 17:42
  • Then you haven't imported the relevant modules – eko Jun 15 '17 at 17:44
  • this is my package.json file. "dependencies": { "@angular/common": "~4.0.0", "@angular/compiler": "~4.0.0", "@angular/core": "~4.0.0", "@angular/forms": "~4.0.0", "@angular/http": "~4.0.0", "@angular/platform-browser": "~4.0.0", "@angular/platform-browser-dynamic": "~4.0.0", "@angular/router": "~4.0.0", "angular-in-memory-web-api": "~0.3.0", "systemjs": "0.19.40", "core-js": "^2.4.1", "rxjs": "5.0.1", "zone.js": "^0.8.4" }, – user269964 Jun 15 '17 at 17:47

1 Answers1

0

Change this line :

From :

<input name='name' [ngModel]="selectedHero?.name" (ngModelChange)="selectedHero.name = $event" />

To :

<input name='name' [ngModel]="selectedHero?.name" (ngModelChange)="selectedHero?.name ? selectedHero.name = $event.target.value : ''" />
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122