サービスからデータを取得するはずのコンポーネントがあります。ここで角度2のコンポーネントが期限内に観測可能なサービスに加入していない
は私のコンポーネントである:ここでは
@Component({
templateUrl: 'app/dashboard/useraccount/firstname/useraccount-firstname.component.html',
directives: [ROUTER_DIRECTIVES],
pipes: [TranslatePipe]
})
export class UserAccountFirstNameComponent implements OnInit {
currentUserAccount:Object;
errorMessage:string;
constructor(private userAccountService:UserAccountService) {
}
ngOnInit() {
this.userAccountService.getCurrentUserAccount()
.subscribe(
param=> this.currentUserAccount = param,
error => this.errorMessage = <any>error
);
}
updateFirstName() {
this.userAccountService.updateFirstName(this.currentUserAccount);
}
}
対応するサービスされています
bootstrap(MainComponent, [ROUTER_PROVIDERS,
HTTP_PROVIDERS,
TRANSLATE_PROVIDERS,
SessionService,
UserAccountService,
TranslateService,
provide(RequestOptions, {useClass: ApplicationRequestOptions}),
provide(LocationStrategy, { useClass: HashLocationStrategy }),
provide(TranslateLoader, {
useFactory: (http:Http) => new TranslateStaticLoader(http, 'assets/i18n', '.json'),
deps: [Http]
})]);
ここに関連する部分である:
@Injectable()
export class UserAccountService {
constructor(private http:Http) {
}
getCurrentUserAccount():Observable<Object> {
return this.http.get('/api/useraccount')
.map(this.mapUserAccount)
.catch(this.handleError);
}
updateFirstName(currentUserAccount) {
this.http.patch('/api/useraccount/firstname/' + currentUserAccount.firstName, null);
}
private mapUserAccount(res:Response) {
console.log(res.json());
return res.json();
}
private handleError(error:any) {
// In a real world app, we might use a remote logging infrastructure
let errMsg = error.message || 'Server error';
console.error(errMsg); // log to console instead
return Observable.throw(errMsg);
}
ここではUserAccountService
のプロバイダがどのようですテンプレートから:
<input type="text"
ngControl="firstName"
#firstName="ngForm"
required
minlength="2"
maxlength="35"
pattern_="FIRST_NAME_PATTERN"
[(ngModel)]="currentUserAccount.firstName"
placeholder="{{'FIRST_NAME_FORM.NEW_FIRST_NAME'| translate }}"
class="form-control"/>
問題は、テンプレートがレンダリングされた時点で、currentUserAccount
はまだngModelでundefined
であるということです...
は誰でも助けてくださいことはできますか?
P.S.角度がngOnInit()
前にすでにバインディングを解決http://plnkr.co/edit/DRoadzrAketh3g0dskpO?p=preview
伝えにくいですが、インスタンス化後にいつでも 'updateFirstName'を呼び出さないようです。また、非同期に処理する必要があります。 'ngOnInit'で行ったのと同じ方法です。それが許されないなら、私には分かります。 – ZenStein
+あなたのサービスの 'updateFirstName'は何も返しません。 – ZenStein
'updateFirstName'はここでは無関係です。関連する 'userAccountService.getCurrentUserAccount'です。 – balteo