递归树,节点正在消失

时间:2017-03-16 10:46:45

标签: angular

我正在尝试使用typeScript + Angular2构建一个简单的递归树。 当我到达第二级节点显示一秒然后消失时,我想有些事件正在清理我的收藏,但我无法弄清楚哪一个。 这是我的代码:

@Component({
 
  selector: 'sp-item-template',
  template:  
  `  {{CurrItem.Title}} 
      <div *ngIf="CurrItem.subItems">
        <ul> 
            <li *ngFor="let item of CurrItem.subItems" (click)="folderClick(item)">                         
                 <sp-item-template [CurrItem] = "item" ></sp-item-template>
            </li>
        </ul>
    </div>
  `
 // templateUrl:'./sp-lists.component.html'
})

export class SpItemTemplate 
{
    @Input() CurrItem:SpItem;
    private listsService: SpListService 
    constructor(@Inject(SpListService) list_service: SpListService){this.listsService = list_service;}

    private folderClick(item: SpItem)
    {
        item.setListService(this.listsService);
        item.GetSubFolders(item.ParentWebRelativeUrl);
    }
}

import { Component, Inject } from '@angular/core';
import {SpListService} from './sp-list.service'

export class SpItem 
{
    Title: string;
    Id: string;
    Name:string;
    RelativeUrl:string;
    Type:string;  
    ParentWebRelativeUrl: string;

    private subItems:SpItem[];

    private listsService: SpListService 
    constructor(@Inject(SpListService) list_service: SpListService){this.listsService = list_service;}

    public setSpItemAsDoclib(title:string, id:string, name:string, parentWebUrl:string):void
    {
        this.Title = title;
        this.Id = id;
        this.Name = name;
        this.ParentWebRelativeUrl = parentWebUrl;
        this.RelativeUrl = parentWebUrl + "/" + name;
    }

    public setSpItemAsFolder(name:string, relativeUrl:string, type:string):void
    {
        this.Name = name;
        this.Title = name;
        this.RelativeUrl = relativeUrl;
        this.Type = type;
    }

    public setListService(ListService:SpListService):void
    {
        this.listsService = ListService;
    }

    public GetSubFolders(parentWeb:string): void
    {
        let hostName = location.protocol + '//' + location.host;
        //this.listsService.getFolders(hostName + "/"+ this.ParentWebRelativeUrl +"/_api/web/GetFolderByServerRelativeUrl('" + this.RelativeUrl +"')/folders")
        this.listsService.getFolders(hostName + "/"+ parentWeb +"/_api/web/GetFolderByServerRelativeUrl('" + this.RelativeUrl +"')/folders")
        .then(listRslt => 
        {
          for (let item of listRslt) 
            {
                item.ParentWebRelativeUrl = parentWeb;
            }
            this.subItems = listRslt
        });
    }
}

1 个答案:

答案 0 :(得分:0)

找到问题:如果节点有孩子,我错过了。 不知道为什么它没有判断孩子们的判断,但如果解决问题就加上这个。

    private folderClick(item: SpItem)
  {
    item.setListService(this.listsService);
    item.setGlobalService(this.globalService);

    **if(item.subItems == null)**
    {
        item.GetSubFolders(item.ParentWebRelativeUrl);
    }     
    item.GetDocuments(item.ParentWebRelativeUrl);
  }