AngularJS Multi-Page App Site Boilerplate Site Structure Advice -
i looking guidance creating angularjs multi-page app served laravel backend. web app tutorials on net point creating spas , getting started angular - please go easy on me.
productpage example - http://example.com/products/widget
<html data-ng-app='exampleapp'> <head> </head> <body data-ng-controller='productcontroller'> // productpage content served laravel angular tags <script type="text/javascript" src="/js/lib/angular.min.js"></script> <script type="text/javascript" src="/js/app.js"></script> <script type="text/javascript" src="/js/controllers/productcontroller.js"></script> </body> </html>
cartpage example - http://example.com/cart
<html> <head> </head> <body data-ng-controller='cartcontroller'> // cartpage content served web-server angular tags <script type="text/javascript" src="/js/lib/angular.min.js"></script> <script type="text/javascript" src="/js/app.js"></script> <script type="text/javascript" src="/js/controllers/cartcontroller.js"></script> </body> </html>
so in above examples, have created 2 pages, served web server pretty static content. pages have been marked angular tags. on each static page, have referenced different angularjs controller.
is right way of tackling problem or should allowing app.js load controllers / inject dependencies?
also, guidance on sharing data between controllers in multi-page app , links decent resources / examples helpful. e.g need pass e.g. items added shopping cart api product page, query api again retrieve cart contents?
you should include ngroute
module , let angularjs load controllers you. please refer angularjs docs find out how work routings.
as sharing data between controllers, services you're looking for. creating service easy this:
app.factory("servicename", [function() { return { somevar: "foo" }; }]);
then, in controllers, inject service this:
app.controller("contactctrl", ["$scope", "servicename", function($scope, svc) { $scope.somevar = svc.somevar; }]);
the service's state retained long don't cause physical page reload (which why should use ngroute
load controllers).
Comments
Post a Comment