How to decide when to use ngView or ngInclude?
Asked Answered
Q

1

46

Just trying to understand the exact difference in approaches of using ngView and ngInclude in AngularJS. When would is it correct to use one over the other? Thanks.

Quaff answered 16/11, 2013 at 23:10 Comment(2)
Routing comes to mind...Plover
simplest explanation...can use ng-include anywhere including nesting in templates. ng-view managed by routing so typically only one existsGelasius
D
40

ngView works together with routing, and is mostly essential to an angular SPA. It's normally used to include the main content of your site, the bit in between the header and footer that changes every time a user clicks a link. Its integration with routing means changing the ngView content changes the controller too (depending on your configuration).

ngInclude is a general purpose include, you may not even need it. You'd sometimes use it inside a view to avoid repeating yourself, in the cases where several views need the same code included.

Decare answered 17/11, 2013 at 9:46 Comment(2)
Cool. Does ngInclude create its own scope or does it inherit the scope of the HTML it is embedded within? If so, doesn't that make re-use hard since the scope variables referenced inside the ngInclude HTML would need to be named the same in all parent HTMLs that want to use it?Riebling
ng-include creates indeed a new child scopePug

© 2022 - 2024 — McMap. All rights reserved.