我是Angular的新手。运行角度代码时,控制台出现以下错误。
AppComponent.html:2 ERROR TypeError: Cannot read property 'colourPreference' of undefined
at AppComponent.get [as colour] (app.component.ts:22)
at Object.eval [as updateDirectives] (AppComponent.html:6)
at Object.debugUpdateDirectives [as updateDirectives] (services.ts:443)
at checkAndUpdateView (view.ts:359)
app.componenet.ts:
import { Component, OnInit } from '@angular/core';
import { UserPreferencesService } from './UserPreferences.service';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<h1>This is the home page</h1>
<div>
Colour preferences:
<input type='text' [(ngModel)]='colour' [style.background]='colour'/>
</div>
`
})
export class AppComponent {
constuctor(private _userPreferencesService: UserPreferencesService){
}
get colour():string{
return this._userPreferencesService.colourPreference;
}
set colour(value: string){
this._userPreferencesService.colourPreference=value;
}
}
UserPreferences.service.ts
import {Injectable} from '@angular/core';
@Injectable()
export class UserPreferencesService{
colourPreference: string ='orange';
}
有人可以解决上述问题吗?谢谢!!