0

At a very abstract level (because the code is way too long to share here), I have a search box in an HTML file and I have a controller for that search box that does some things with the text entered in the text field. Now I want to pass the search results to a new HTML page just for the search results but for some reason this isnt working. I have the ng-controller directive in both HTML files and everything should be syntactically correct.

This worked fine when I used ng-include to include the second HTML file in the first one, but when I separated them I cant see the search results. I read somewhere on SO that separating the HTML pages as I have done creates a new $scope which is why when I use $scope.searchTerm in the controller only one of the HTML pages can see searchTerm, but I'm not sure if this is the problem.

ninesalt
  • 4,054
  • 5
  • 35
  • 75
  • 1
    If the code is too long, write up a snippet (jsFiddle?) that demonstrations the issue you are having. Need code to debug code.. – Tim Grant May 19 '16 at 23:49

1 Answers1

0

Use a service to store the value, that way the value can be read from the service into separate controllers. An Angular service is a singleton and is a great way to store data like that.

The reason ng-controller is not working on a separate HTML file, is because it will re-load the controller, thus bringing it to its initial state, instead of the one after the edits you are hoping to pick up.

jadams
  • 195
  • 1
  • 8
  • Take a look at this post for a description of controller life cycle http://stackoverflow.com/questions/16094940/what-is-the-lifecycle-of-an-angularjs-controller – jadams May 19 '16 at 23:53
  • Is there a built-in service I can use for this? If not, would you mind linking me to a good example of this (that's not the documentation)? Also, how would I use the service in the second HTML file? Its still linked to the same controller. I dont really understand how this would work. – ninesalt May 20 '16 at 00:00
  • I'm hoping that the accepted answer means you were able to figure it out? Sorry, I was away from my computer until now. – jadams May 20 '16 at 17:20
  • Yep I figured it out. I just made another controller and passed what I needed through the $routeParams. Thanks! – ninesalt May 20 '16 at 22:10