Ho due AngularJS 2.0 Componenti:"ECCEZIONE: il valore di direttiva imprevisto 'undefined' sulla vista di componente" a componenti nidificati
Commento:
import {Component, Injectable, View, Input} from 'angular2/core';
import {Comments} from './comments.component';
@Injectable()
@Component({
selector: 'comment'
})
@View({
templateUrl: 'res/templates/components/comment.component.html',
directives: [Comments]
})
export class Comment {
@Input() comment;
@Input() commentable = false;
}
Commenti:
import {Component, Injectable, View, Input} from 'angular2/core';
import {CommentsService} from './../services/comments.service';
import {RouteParams} from 'angular2/router';
import {Comment} from './Comment.component';
@Injectable()
@Component({
selector: 'comments',
providers: [CommentsService]
})
@View({
templateUrl: 'res/templates/components/comments.component.html',
directives: [Comment]
})
export class Comments {
@Input() ID;
public comments;
public commentsService: CommentsService;
public routeParams: RouteParams;
constructor (routeParams: RouteParams, commentsService: CommentsService) {
var self = this;
self.routeParams = routeParams;
self.commentsService = commentsService;
}
ngOnInit() {
var self = this;
if (self.ID !== undefined)
self.comments = self.commentsService.comments[self.ID];
else if (self.routeParams.params['id'] !== undefined)
self.comments = self.commentsService.comments[self.routeParams.params['id']];
else
self.comments = undefined;
}
}
comment.template:
<div class="post">
<div class="author-pic"></div>
<div class="body">
<h2 class="title">{{comment.author.name}} {{comment.author.surname}}</h2>
<h3 class="title">{{comment.publication | date:"MM/dd/yy"}}</h3>
<p>{{comment.body}}</p>
</div>
<comments *ngIf="commentable" [ID]="comment.ID"></comments>
</div>
comments.template:
<div *ngIf="comments !== undefined">
<comment *ngFor="#comment of comments" [comment]="comment" [commentable]="true"></commento>
</div>
Nel modello Commenti Ho un ciclo angolare che consente di stampare più componenti commento. Nel modello dei commenti ho un ngIf angolare che, se var commentable è vero, stampa un componente commenti. Quando l'eseguo ottengo:
EXCEPTION: Unexpected directive value 'undefined' on the View of component 'Comment'
Potreste inviare i modelli associati, e il servizio (s)? Perché hai aggiunto il decoratore *** @ Injectable *** sopra i componenti? – Romain
Molto probabilmente perché si ha una dipendenza circolare. Commenti hanno commenti come direttive e Commenti ha anche commenti come direttive. Rimuovi uno di quelli dall'altro. –
Se rimuovo uno di quelli non funzionerebbe ... @Eric Martinez – Samuele