尝试使用嵌套组件时未承诺的异常

时间:2016-04-08 05:27:02

标签: angular angular2-template angular2-directives

我在尝试使用嵌套组件时遇到此异常:

EXCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property 'endSourceSpan' of null
XCEPTION: Error: Uncaught (in promise): TypeError: Cannot set property 'endSourceSpan' of nullBrowserDomAdapter.logError @ angular2.dev.js:23877BrowserDomAdapter.logGroup @ angular2.dev.js:23888ExceptionHandler.call @ angular2.dev.js:1317(anonymous function) @ angular2.dev.js:12763schedulerFn @ angular2.dev.js:13167SafeSubscriber.__tryOrUnsub @ Rx.js:10775SafeSubscriber.next @ Rx.js:10730Subscriber._next @ Rx.js:10690Subscriber.next @ Rx.js:10667Subject._finalNext @ Rx.js:11191Subject._next @ Rx.js:11183Subject.next @ Rx.js:11142EventEmitter.emit @ angular2.dev.js:13148NgZone._zoneImpl.ng_zone_impl_1.NgZoneImpl.onError @ angular2.dev.js:13566NgZoneImpl.inner.inner.fork.onHandleError @ angular2.dev.js:2128ZoneDelegate.handleError @ angular2-polyfills.js:336Zone.runGuarded @ angular2-polyfills.js:244drainMicroTaskQueue @ angular2-polyfills.js:495ZoneTask.invoke @ angular2-polyfills.js:434
angular2.dev.js:23877 STACKTRACE:BrowserDomAdapter.logError @ angular2.dev.js:23877ExceptionHandler.call @ angular2.dev.js:1319(anonymous function) @ angular2.dev.js:12763schedulerFn @ angular2.dev.js:13167SafeSubscriber.__tryOrUnsub @ Rx.js:10775SafeSubscriber.next @ Rx.js:10730Subscriber._next @ Rx.js:10690Subscriber.next @ Rx.js:10667Subject._finalNext @ Rx.js:11191Subject._next @ Rx.js:11183Subject.next @ Rx.js:11142EventEmitter.emit @ angular2.dev.js:13148NgZone._zoneImpl.ng_zone_impl_1.NgZoneImpl.onError @ angular2.dev.js:13566NgZoneImpl.inner.inner.fork.onHandleError @ angular2.dev.js:2128ZoneDelegate.handleError @ angular2-polyfills.js:336Zone.runGuarded @ angular2-polyfills.js:244drainMicroTaskQueue @ angular2-polyfills.js:495ZoneTask.invoke @ angular2-polyfills.js:434
angular2.dev.js:23877 Error: Uncaught (in promise): TypeError: Cannot set property 'endSourceSpan' of null
    at resolvePromise (angular2-polyfills.js:543)
    at angular2-polyfills.js:520
    at ZoneDelegate.invoke (angular2-polyfills.js:332)
    at Object.NgZoneImpl.inner.inner.fork.onInvoke (angular2.dev.js:2111)
    at ZoneDelegate.invoke (angular2-polyfills.js:331)
    at Zone.run (angular2-polyfills.js:227)
    at angular2-polyfills.js:576
    at ZoneDelegate.invokeTask (angular2-polyfills.js:365)
    at Object.NgZoneImpl.inner.inner.fork.onInvokeTask (angular2.dev.js:2103)
    at ZoneDelegate.invokeTask (angular2-polyfills.js:364)BrowserDomAdapter.logError @ angular2.dev.js:23877ExceptionHandler.call @ angular2.dev.js:1320(anonymous function) @ angular2.dev.js:12763schedulerFn @ angular2.dev.js:13167SafeSubscriber.__tryOrUnsub @ Rx.js:10775SafeSubscriber.next @ Rx.js:10730Subscriber._next @ Rx.js:10690Subscriber.next @ Rx.js:10667Subject._finalNext @ Rx.js:11191Subject._next @ Rx.js:11183Subject.next @ Rx.js:11142EventEmitter.emit @ angular2.dev.js:13148NgZone._zoneImpl.ng_zone_impl_1.NgZoneImpl.onError @ angular2.dev.js:13566NgZoneImpl.inner.inner.fork.onHandleError @ angular2.dev.js:2128ZoneDelegate.handleError @ angular2-polyfills.js:336Zone.runGuarded @ angular2-polyfills.js:244drainMicroTaskQueue @ angular2-polyfills.js:495ZoneTask.invoke @ angular2-polyfills.js:434
angular2-polyfills.js:469 Unhandled Promise rejection: Cannot set property 'endSourceSpan' of null ; Zone: angular ; Task: Promise.then ; Value: TypeError: Cannot set property 'endSourceSpan' of null(…)consoleError @ angular2-polyfills.js:469drainMicroTaskQueue @ angular2-polyfills.js:498ZoneTask.invoke @ angular2-polyfills.js:434
angular2-polyfills.js:471 Error: Uncaught (in promise): TypeError: Cannot set property 'endSourceSpan' of null(…)consoleError @ angular2-polyfills.js:471drainMicroTaskQueue @ angular2-polyfills.js:498ZoneTask.invoke @ angular2-polyfills.js:434

这是我的主要组件代码:

<base-menu [menu-items-list]="menuList" [menu-layer]="layer"
    (select-menu-item)="selectMenuItem($event)"
    (add-new-item)="addMenuItem($event)"></base-menu>
import {BaseMenuComponent} from '../../../../components/menuComponent/baseMenuComponent/baseMenuComponent';

let applicationPath: string = '/app/pages/adminPage/categoriesPage/requestsPage';

@Component({
    selector: 'companies-Page',
    templateUrl: applicationPath + '/requestsPage.html',
    styleUrls:[ applicationPath + '/requestsPage.css'],
    providers:[RequestTypeService, HTTP_PROVIDERS], 
    directives:[BaseMenuComponent]
})
export class RequestsPage implements OnInit {
    requestTypes:Array<RequestType> = new Array();
    searchQuery: string = "";
    menuList = [
        {id:12, layer:0, name:'asd'},
        {id:13, layer:0, name:'asda'},
        {id:14, layer:0, name:'asdd'}];
    layer = 0;
    _requestTypeService:RequestTypeService

    constructor(requestTypeService:RequestTypeService) {
        this._requestTypeService = requestTypeService;
    }

    ngOnInit(){
        //this.getRequestTypesWithFilters();
    }
}

这是子组件:

import {Component, Input, Output, EventEmitter, OnInit} from 'angular2/core';

@Component({
    selector: 'base-menu',
    //templateUrl: '/app/components/menuComponent/baseMenuComponent/baseMenuComponent.html'
    template:`
    <div class="base-menu-component">
        <ul class="nav nav-pills nav-stacked">
            <li class="form-group row">
                <input class="form-control" [(ngModel)]="newMenuItem"/>
                <button class="btn btn-success" (click)="addMenuItem()">
                    <span class="glyphicon glyphicon-plus"></span>
                </button>
                <button class="btn btn-success" (click)="clearMenuItem()">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
            </li>
            <li *ngFor="#item of menuItems" [class]="isItemSelected(item) ? 'active':''" (click)="selectItem(item)">
                <a>{{item.name}}</a>
            </li>
        </ul>
    </div>`
})

export class BaseMenuComponent implements OnInit {
    @Input('menu-items-list') menuItemsList:Array<MenuItem>;
    @Input('menu-layer') menuLayer:number;
    @Output('select-menu-item') broadcastMenuItem: EventEmitter<MenuData> = new EventEmitter<MenuData>();
    @Output('add-new-item') broadcastNewItem: EventEmitter<MenuData> = new EventEmitter<MenuData>();

    selectedItem:MenuItem;
    newMenuItem:string;

    constructor() { }

    ngOnInit(){
        this.selectedItem = this.menuItemsList && this.menuItemsList.length > 0 ? this.menuItemsList[0] : null;
        this.newMenuItem = "";
    }

    isItemSelected(menuItem){
        return this.selectedItem == menuItem;
    }

    selectItem(menuItem){
        this.selectedItem = menuItem;
        this.broadcastMenuItem.emit({menuItem:menuItem, menuLayer:this.menuLayer});
    }

    addMenuItem(){
        this.broadcastNewItem.emit({menuItem:this.newMenuItem, menuLayer:this.menuLayer});
        this.clearMenuItem();
    }

    clearMenuItem(){
        this.newMenuItem = "";
    }
}

interface MenuItem{
    id;
    layer;
    parentId;
    name;
}

interface MenuData{
    menuItem;
    menuLayer;
}

有人可以解释一下这个错误在哪里以及它出现的原因吗? 对我来说,html和两个组件看起来都非常清晰。

7 个答案:

答案 0 :(得分:29)

我确实遇到了类似的问题:

我使用angular-cli构建了一个小应用程序,当创建组件的html时,它有类似的东西:

<p>
   Component works !
</p>

所以我所做的就是测试我的组件:

<p>
   Component works !

   <div *ngFor="#value in values">
      {{value}}
   </div>
</p>

然后错误出现了。

这只是因为段落不能包含像(div,h1,ul等)这样的块元素。

这么多浪费时间只是为了发现......

当我这样做时

 <div>
    Component works !

    <div *ngFor="#value in values">
       {{value}}
    </div>
 </div>

工作正常;)

答案 1 :(得分:14)

也可能意味着你错过了收盘双重报价

<div (click)="myClick()>What's up with my CODE!???</div>

答案 2 :(得分:6)

我有类似的问题。我收到的错误是&#34; EXCEPTION:TypeError:无法设置属性&#39; endSourceSpan&#39; of null&#34; (见下图)。在我的模板中,我错过了结局&#34;&gt;&#34;对于div(见下图)。一旦我添加&#34;&gt;&#34;,错误便消失了。

我相信Angular 2模板中任何格式错误的HTML都会出现此错误。

Angular 2 error - Cannot set property 'endSourceSpan' of null

Angular 2 - Missing Ending Bracket on Div

答案 3 :(得分:4)

我遇到了同样的问题,它发现它是一个我没有关闭的HTML标签..非常难找到

答案 4 :(得分:3)

如果<xxx />是无效元素

,则

xxx有效

  

area,base,br,col,command,embed,hr,img,input,keygen,link,meta,param,source,track,wbr

https://www.w3.org/TR/html-markup/syntax.html#syntax-elements

  

不,没关系,我发现这个bug是html中未公开的div,

错过了关闭<div>

<击> 大概

<击>
<input class="form-control" [(ngModel)]="newMenuItem"/>

/>无效HTML5。 Angular对你传递给Angular的HTML非常严格。

<input>

  

标记省略必须有一个开始标记,且不得有结束标记。

将其更改为

<input class="form-control" [(ngModel)]="newMenuItem">

<击>

答案 5 :(得分:0)

我遇到了同样的问题,结果发现我错过了HTML标记中的赋值运算符(即<div class"cf">而不是<div class="cf">

答案 6 :(得分:0)

我有这个问题,因为我违反了我不了解的HTML法律。

这是禁止html的一个例子: 因为你在p元素中有ul元素,而在html法则中是禁止的

<p>this will not work <ul><li *ngFor="#item on items">{{item.name}}</li></ul></p>

但这会奏效:

<p>This will definitely work </p>
<ul><li *ngFor="#item on items">{{item.name}}</li></ul>

当我与angulars dev谈论这件事时,他们告诉我必须采取艰难的方式

https://github.com/angular/angular.js/issues/15139#issuecomment-247256778

当然你会在angularjs1和angularjs2中拥有它。