[英]Angular multiple nested controllers
我有一個看似常見的問題,但我無法在網上找到如何實現我想做的事情。
我有一個搜索表單,其中包含一個公共控制器的多個實例,我用它來鍵入時執行先行/自動完成搜索。 每個控制器配置有不同的參數,以使用不同的標准進行查找。 需要將查找結果分配給父屬性,以根據輸入的條件執行搜索。 例如:
SearchController
CleanerId
TeacherId
StudentId
並且子控制器只具有帶有Id和Text值的查找控件的概念,其中需要將Id分配給適當的父屬性。
理想情況下,我想將每個控制器的隱藏id字段綁定到子模型的id和父節點上的不同屬性,如下所示,但我不認為這是可能的:
<input type =“hidden”ng-model =“child.Id,parent.CleanerId”/>
<input type =“hidden”ng-model =“child.Id,parent.TeacherId”/>
<input type =“hidden”ng-model =“child.Id,parent.StudentId”/>
子控制器必須是通用的,那么如何讓父控制器綁定到子ID呢?
任何幫助是極大的贊賞。
這是為三個小部件使用指令而不是控制器的絕佳機會。
使用該指令時,將該字段作為參數傳遞:
<child-widget value="parent.CleanerId"></child-widget>
<child-widget value="parent.TeacherId"></child-widget>
<child-widget value="parent.StudentId"></child-widget>
在定義指令時,傳遞隔離范圍中的值:
app.directive("childWidget", function () {
return {
restrict: "E",
scope: {
value: "="
},
template: '<input type="hidden" ng-model="value" />'
};
});
這是一個完美的博客,以獲得有關父子控制器通信的答案:
https://rclayton.silvrback.com/parent-child-controller-communication
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.