【问题标题】:Template Error on ngFor and using pipe with parameterngFor 上的模板错误和使用带参数的管道
【发布时间】:2017-10-05 12:41:52
【问题描述】:

我正在尝试在 Angular 4 上使用管道进行排序。管道和 ngFor 没有问题。

 <div *ngFor="let item of data | paginate: { itemsPerPage: 8, currentPage: p } |  orderBy : ['{{config}}'] " class="col-lg-3 col-md-6 col-sm-6">
            <blog-thumb [date]="item.date" [commentCount]="item.commentCount"> </blog-thumb>
        </div>

像上面一样,有 2 个我自己开发的管道。使用该语法存在错误。换成这个就没有问题了

这是我的组件类。

export class MainBlogPageComponent implements OnInit {

    public sortOrder: string = "asc";
    public config: string = "-commentCount";

    private data: any;
    private blogPosts: string;
    constructor() {       
     }
    ngOnInit() { }
}

还有我所有的模块CommonModule 导入,BrowserModule App.Module.ts 也导入了

这是在 chrome 浏览器上看到的异常。

Can't bind to '*ngFor' since it isn't a known property of 'div'. ("
            </div>
        </div>
        <div [ERROR ->]*ngFor="let item of data | paginate: { itemsPerPage: 8, currentPage: p } |  orderBy : ['{{config}}'] "): ng:///MainBlogModule/MainBlogPageComponent.html@37:13
Error: Template parse errors:
Can't bind to '*ngFor' since it isn't a known property of 'div'. ("
            </div>
        </div>
        <div [ERROR ->]*ngFor="let item of data | paginate: { itemsPerPage: 8, currentPage: p } |  orderBy : ['{{config}}'] "):

它不像模板解析错误等其他异常。没看懂问题出在哪里。

这是MainBlogPageComponent 导入的我的模块。

@NgModule({
    imports: [CommonModule,
    FormsModule,
    NgaModule,
    routing,
    NgxPaginationModule
    ],
    exports: [],
    declarations: [MainBlogComponent,
    MainBlogPageComponent,
    SubHeaderComponent,
    BlogThumbComponent],
    providers: [],
})
export class MainBlogModule { }

我的共享模块中的管道,我所有的管道都位于共享模块和共享模块中。我使用了另一个没有问题的模块。

【问题讨论】:

  • 可以在定义MainBlogPageComponent 的地方添加@NgModule 配置吗?
  • 已更新,希望一切正常。谢谢。
  • 尝试从ngFor移除管道
  • 正如我在问题上所说的那样,当我更改为像“+commentCount”这样的静态参数时,它就好了。我只想使用参数。

标签: angular angular2-directives angular2-pipe


【解决方案1】:

我觉得应该是

orderBy : [config]

这样在您的OrderBy 管道中您将获得["-commentCount"] 参数

【讨论】:

  • 它很好,但我不明白。我不使用 "{{ }}" 来绑定吗?我在 angular.io 上看到了教程
  • 您不应将插值与[] 一起使用*ngFor[ngForOf] 的语法糖[...] 将值解释为表达式。
猜你喜欢
  • 2017-12-10
  • 2017-02-02
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
相关资源
最近更新 更多