2015-05-19 18 views

回答

1

Angular內部使用for..in運算符遍歷非數組對象。 Proof-link to the sourcesMapSet不能通過這種方式迭代,它們應該通過for..of迭代。所以你不能使用這個集合ng-repeat沒有任何額外的轉換。

即將到來的Angular 2有ES6功能的支持,它也有for..of語法重複。有關更多詳細信息,請參閱angular 2 docs

0

我試圖在Angular 2中綁定Mapng-repeat。即使使用*ngFor="let item of items",它也不起作用。相反,這解決了我的問題:

/* 
* Extension of the ES6 map, which also supports binding to it's array of values 
*/ 
class BindableMap extends Map { 

    constructor(...args) { 
     super(...args); 
     this.bindableValues = []; 
    } 

    updateBindableValues() { 
     // Clear the array without creating a new one. 
     this.bindableValues.length = 0; 
     // Push all the new values 
     this.bindableValues.push(...Array.from(super.values())); 
    } 

    clear(...args) { 
     super.clear(...args); 
     this.updateBindableValues(); 
    } 

    set(...args) { 
     super.set(...args); 
     this.updateBindableValues(); 
    } 

    delete(...args) { 
     super.delete(...args); 
     this.updateBindableValues(); 
    } 
} 

這樣你就可以用ng-repeat="value in map.bindableValues"也使用它的角1.x中。

請注意,這不是最有效的解決方案,因爲每次更改地圖時都會重新創建可綁定值。儘管如此,我的目的是足夠的。另外,如果您願意,還可以將BindableMap擴展爲綁定到鍵或鍵值對。