我打算在具有不同控制器的多個視圖中使用一個模板。
在ControllerAs語法中使用ngInclude的最佳實踐是什麼?
但現在我意識到我不能在模板中編寫通用綁定,因爲值將被放在$scope.concreteControllerName
之內。
爲ngInclude角文檔說
此指令創建新的範圍。
我可以用ng-init
指令,並通過控制器實例模板的範圍:
<ng-include src="..." ng-init="controller=concreteControllerName"/>
甚至更好
<ng-include src="..." ng-init="model=getModelForTemplate()"/>
,然後在模板寫{{controller.boundvalue}}
。
這是一個工作解決方案,我猜。
在這裏我想知道是否有其他更好的方法存在,如果不是,應該使用模板總是與傳遞模型的一些概念來抽象遠離父範圍?
看看這個[SO問題](http://stackoverflow.com/questions/13422966/how-to-specify-model-to-a-nginclude-directive- in-angularjs/20639139#20639139) - 幾個答案都很棒。 – 2014-10-17 15:28:19