我正在嘗試爲應用程序構建模板並希望顯示帶有名稱的動態列表。所以我得到這個代碼來顯示列表並添加/刪除行;Ng-init如果數組尚未創建
<table ng-init="page.businessRows = []">
<thead>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Phone</th>
</tr>
</thead>
<tr ng-repeat="row in page.businessRows">
<td>
<input type="text" ng-model="row.name" />
</td>
<td>
<input type="text" ng-model="row.contact" />
</td>
<td>
<input type="text" ng-model="row.phone" />
</td>
<td>
<button ng-click="page.businessRows.splice($index,1)">
Remove
</button>
</td>
</tr>
</table>
<button class="btn" ng-click="page.businessRows.push({})">addRow</button>
的事情,當這個模板被加載page.busnessRows將最有可能加載行,所以我想改變ng-init
僅創建空數組,如果businessRows將不會被初始化。我試過ng-init="page.businessRows = page.businessRows.length < 1 ? [] : page.businessRows
但它沒有工作。我如何打算以流利的語言表達條件?
所有幫助表示讚賞。在此先感謝
這解決了他問到的問題。但我認爲它不能解決嘗試使用View作爲控制器的更大問題。 –
我明白你的觀點。我也很少使用ng-init。也就是說,我不能爲每個人和他們的需求說話。畢竟,指令在那裏,如果有人認爲它有助於解決他們的問題,對他們有好處。 –