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