Dynamically Loading Controllers and ng-include

前端 未结 3 509
夕颜
夕颜 2021-01-31 16:57

At the moment I have an app that has a sidebar, and the sidebar loads different html templates using ng-include based on what operation the user chooses to do. It\'

3条回答
  •  有刺的猬
    2021-01-31 17:26

    I've forked your plunkr demo into one that I believe does what you're looking for: http://plnkr.co/edit/whsjBT?p=preview

    This demonstrates an event being broadcast from one controller to another AFTER the 2nd controller (LegCtrl in our example here) is loaded via ng-include and passing it data.

    I used $includeContentLoaded event from ng-include to delay broadcasting the event until angular reports that add_leg.html is loaded. Also I think there were some issues with how you were using $broadcast()... it's first parameter should be the same as the one used in $on() in LegCtrl

    Another idea to consider is simply using your Navigation service itself to share state between the controllers if that's appropriate in your scenario.

提交回复
热议问题