Where to make http request for every route in angularjs? -
suppose web app needs make http request site title, site description , on. since these variables common pages, makes sense request every time user enter site.
the question is, make calls? in run block? or create root controller these tasks?
you can use 1 of these 2 approaches:
- make call in run block , store value in
$rootscope
, use anywhere want, in states, use
resolve
page title , details , , in views , ease use resolve in root state , use resolved variable dependency in other child or sibling routes values..$stateprovider.state('root', { resolve:{ // example using function simple return value. promiseobj: function($http){ // $http returns promise url data return $http({method: 'get', url: '/someurl'}); } }) .state('sibling',{ controller:function($scope,promiseobj){ $scope.title = promiseobj.title; } })
Comments
Post a Comment