I'm working on Angular seed in my project.
I have two identical views(HTML pages) which are same (having same elements and functionality).
These two pages have GridView that are to be populated by same service as well but only the REST API URL is different for both of these pages. Now in my router.js, I want to use $stateprovider.state() to route to these pages.
My question is: Can I use a single controller for both these views or a separate controller for each. But, when I use the same controller for both view pages I get "Injector not defined" error.
Is there a different way to do it? What is the best procedure to handle such conditions?
-
can you provide some code? :)Linh Pham– Linh Pham2015-02-28 17:56:55 +00:00Commented Feb 28, 2015 at 17:56
-
You're using ui-router I gather?Kjell Ivar– Kjell Ivar2015-02-28 18:35:08 +00:00Commented Feb 28, 2015 at 18:35
-
you can wrap your grid into a directive (the directive could contain its own controller) then reuse this directive on your viewsMarian Ban– Marian Ban2015-02-28 18:41:14 +00:00Commented Feb 28, 2015 at 18:41
1 Answer
This is a bit of a generic answer, I can update it if you provide some additional details or code. It's possible to use the same controller for different views. Although personally, I tend to try to avoid this, in case I later have to mod one of the views. I would use two different controllers, but put a lot of common code in a service/directives. I think it's a good practice to minimize the amount of business logic in your controllers.
If you have identical views, that only change an url, I would consider using an url parameter or route parameter for this. Then you would in reality only have one view and one controller.