我有两个AngularJS 2.0组件:
评论:
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;
}
Run Code Online (Sandbox Code Playgroud)
评论:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
comments.template:
<div *ngIf="comments !== undefined">
<comment *ngFor="#comment of comments" [comment]="comment" [commentable]="true"></commento>
</div>
Run Code Online (Sandbox Code Playgroud)
在评论模板中,我有一个Angular循环,可以打印多个Comment组件.在Comment的模板中,我有一个Angular ngIf,如果var commentable为true,则打印一个Comments组件.当我运行它时,我得到:
EXCEPTION: Unexpected directive value 'undefined' on the View of component 'Comment'
Run Code Online (Sandbox Code Playgroud)
use*_*727 18
有一种方法可以解决循环依赖问题.
import {forwardRef} from 'angular2/core';
import {Comment} from './Comment.component';
...
directives: [forwardRef(() => Comment)]
})
Run Code Online (Sandbox Code Playgroud)
我试过这个,2.0.0-beta.10它工作正常.有关更多信息,请参阅本期 github上的问题报告中的tlancina发表的评论.
Sam*_*nes 12
循环TypeScript导入依赖项可能导致此问题.(我刚碰到这个)
为了我:
A是父组件,imports和exports B,C,D.为了方便.
A尝试渲染B
乙进口Ç 从 一个,并试图呈现Ç.
一旦我将C添加到B的directives块中,我就会得到该错误(无论是否在B的模板中.)实际上,因为A依赖于B,所以我将A中的任何内容导入B的块I得到
意外的指令值'undefined'
@Eirc Martinez是对的,你必须找到解决循环依赖的方法.
当我导入一些不存在的东西时,我经常会遇到这个错误.通常这意味着我的导入中存在拼写错误或具有相同性质的东西.
例如,当我尝试导入时出现此错误:
import {Comment} from './Comment.component';
Run Code Online (Sandbox Code Playgroud)
当我的意思是:
import {CommentComponent} from './Comment.component';
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8568 次 |
| 最近记录: |