0

I've been going over this SO answer regarding $scope inheritance and then set it up in my app. However, it seems that by using this method it also inherits the template of the parent - which won't work for my use case. So it seems the other option available to me would be to set up a $scope service and inject that into my controller?

I just want to be able to use 1 controller but have a few different templates using that controller, is a $scope service the better way to go?

Community
  • 1
  • 1
user3125823
  • 1,846
  • 2
  • 18
  • 46
  • this is, in my opinion, a completely wrong approach to use in angular. Don't re-invent the wheel and try to override how `$scope` works, that's only asking for trouble, and makes your code less understandable to others. Along that same line, you should stick with the paradigm of MVC as much as possible, i.e. one Model matched to a single View with a specific Controller. Don't make mega-controllers. – Claies Oct 05 '16 at 15:44
  • @Claies ok point taken. What I'm trying to achieve is this. I have a niche search application. 2 templates - one for entering query/autocomplete - the other for results and more searching and 1 controller. How would you set that up? Suggestions are welcomed and thanks in advance. – user3125823 Oct 05 '16 at 15:55
  • What does "results and more searching" mean? My first instinct based on what you are describing would be to make a search directive, and bind the results of the directive to the display, but again, I would never consider sharing a controller between multiple views; I think the ideal way to work in angular is to embrace the idea of composition, and have a controller per logical component, rather than a controller per page(s). – Claies Oct 05 '16 at 16:02
  • @Claies "results and more searching" just means that it displays results and provides a search bar if they want/need to search again. I'm just trying to keep things simple so I figured one controller since both pages do very similar things. However, I'm having difficulty with the $scope inheritance part of this and need a practical solution. – user3125823 Oct 05 '16 at 16:08
  • it *sounds* like they are the same page? wouldn't you just have a single template with the search form on the top and the results under it, and the results are just not rendered if they are null? – Claies Oct 05 '16 at 16:10
  • @Claies yes they are very similar. Basically the only difference is that one page is just to enter a query when they arrive to the site and the results page is just for that - to show results and provide for more searching. I want to have the home page just for the initial query and then show results on the second page - what would be your suggestion for that setup and have $scope inheritance from one page to the second page? – user3125823 Oct 05 '16 at 16:14

0 Answers0