angularjs - auto run a service function when a page is visited -


the routing.js code below injects given page using $routeprovider. each page has it's own controller uses service togglefactory.

is there way automatically fire function "menutoggle" of service "togglerfactory" whenever page visited? thanks

//---routing.js----------------------------  (function () {      'use strict';      angular          .module('appmodule')          .config(['$routeprovider', routing]);        function routing ($routeprovider) {          $routeprovider              .when('/page1', {              url: "/page1",              templateurl: 'views/page1.html',              controller: 'page1ctrl'          })              .when('/page2', {                  url: "/page2",                  templateurl: 'views/page2.html',                  controller: 'page2ctrl'              })      }  })();    //---page1ctrl.js------------------------  (function () {      'use strict';      angular          .module('appmodule')          .controller('page1ctrl', ['$scope', 'togglerfactory', page1ctrl]);        function page1ctrl($scope, toggler) {          $scope.menutoggle = toggler.menutoggle;      }  })();    //---menutoggle.js-----------------------  (function () {      'use strict';        angular.module('appmodule')          .factory('togglerfactory', function($rootscope) {              var togglerfactory = {};                togglerfactory.menutoggle = function(){                  $rootscope.clicked = !$rootscope.clicked;              }                return togglerfactory;          });  }());

you injecting "togglerfactory" controller, do

toggler.menutoggle() inside controller 

controller in general

var appmodule = angular.module('appmodule', []);  appmodule.service('togglerfactory', function() {     return {         menutoggle : function() {             alert("inside togglefactory");         }     } })  appmodule.controller('page1ctrl', function($scope, $http, togglerfactory) {     togglerfactory.menutoggle(); }); 

for controller

   appmodule.controller('page1ctrl', ['$scope', 'togglerfactory', page1ctrl]);      function page1ctrl($scope, toggler) {         toggler.menutoggle();     } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -