在ng2组件上设置成员变量时遇到问题

时间:2017-07-28 00:21:08

标签: javascript angular typescript

我在ng2组件上设置成员变量时遇到问题。在下面的组件中,我试图将一些服务结果设置为成员变量。 updateSearchResults1()按预期设置成员变量,但updateSearchResults2将promise委托给processSearchResults,后者返回错误:“TypeError:无法将属性'blogPostListItems'设置为null。”我的理解是这两个实现在功能上是相同的。那么为什么我可以从updateSearchResults1()设置this.blogPostListItems,而我从updateSearchResults2()获得this.blogPostListItems为空的错误?

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Router } from '@angular/router';
import { BlogService } from '../../services/blog.service';
import { BlogLanding } from '../../entities/blog-landing';
import { Response } from '@angular/http';

@Component({
    selector: 'blog-landing',
    templateUrl: '../../../ng2/templates/blog-landing.component.html'
})

export class BlogLandingComponent implements OnInit
{
    @ViewChild('ddlAuthor') ddlAuthor;
    blogLanding: BlogLanding;
    blogPostListItems: Array<Object>;

    constructor(
        private router: Router,
        private blogService: BlogService){}

    ngOnInit(): void
    {
    }

    updateSearchResults1()
    {
        this.blogService
            .getBlogPosts()
            .then(blogPostListItems => this.blogPostListItems = blogPostListItems)
    }

    updateSearchResults2()
    {
        this.blogService
            .getBlogPosts()
            .then(this.processSearchResults);
    }

    processSearchResults(responseObject)
    {
        this.blogPostListItems = responseObject;
    }

}

0 个答案:

没有答案