AngularJS - How to execute function in controller only once in the context of Routing -


i'm using routing feature of angularjs. furthermore want on controller templates.

in controller angularjscontroller have initfunction, should executed @ first time controller gets executed , when explicitly call function. don't wan't init function executed each time routing loads template.

how can achieve desired behaviour?

var app = angular.module("angularjsapplication", ["ngroute"]);  app.config(function($routeprovider) {    $routeprovider        .when("/overview", {            templateurl : "overview.html",            controller : "angularjscontroller"        .when("/settings", {            templateurl : "settings.html",            controller : "angularjscontroller"        ... });  app.controller("angularjscontroller", function ($scope, $location, $http) {   $scope.init = function() {     //do stuff   }; } 

create service things should executed once. services stored singletons, can provide 1 or more ways re-execute code.

app.factory("initservice", [function() {     var returnobject = {}     // set properties of returnobject     // if need "explicitly call function"     returnobject.recaculate = function() {         // perform recaculations     }      return returnobject; }]); 

don't forget inject service controller(s):

app.controller("angularjscontroller", ["$scope", "$location", "$http", "initservice", function ($scope, $location, $http, initservice) {     // controller contents     $scope.reinit = function() {        initservice.recaculate();     } }]); 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -