angularjs - how to call a function declared into controller and need to call it into app.run block? -


i have written function in pagechanged() controller. , have call function app.run block. mentioned parameter $rootscope, $scope etc. gives me error type error. trying implement logic browser button in app.run block,it gives me error. error line mentioned in code through comment. code here:

var app=angular.module('myapp', ['ngroute','ngcookies','ui.bootstrap','ui.router','ngstorage']);     console.log("in appnew.js")      app.config(function($routeprovider,$locationprovider,$httpprovider) {         $locationprovider.html5mode(true);         $routeprovider         .when('/',{             templateurl:  'login_admin.html'          })          .when('/add',{             templateurl: 'add_brands.html',             controller: 'dashctrl',             authenticated: true,             reloadonsearch: false          })            .otherwise({             redirectto: "/"         });     });       app.factory('usermodel', function($http,$cookies,$location){         var usermodel={};          usermodel.login= function(loginfrm){           console.log("loginfrm"+loginfrm.user_name);           $http.post("http://.............../admin_login",loginfrm).           success(function(response){             if (response.success == 1) {                 console.log("password matched");                 $cookies.put('user_name',loginfrm.user_name);                 $cookies.put('token',response.token);                 $location.path('/dashboard');             } else if (response.success == -1) {                 console.log("password not matched");             } else {                 console.log("sorry there error");              }            /*console.log('$scope.dynamic1: %j', $scope);*/            console.log("response success: %j",response)            }).           error(function(response){             console.log("response error:",response);           });         };         usermodel.getauthstatus = function(){           var status = $cookies.get('user_name');           console.log('status: %j', status);           if(status){             return true;           }           else{             return false;           }         };         console.log('usermodel.getauthstatus'+usermodel.getauthstatus());         usermodel.douserlogout = function(){           $cookies.remove('user_name');            $cookies.remove('token');         }         console.log("usermodel: %j",usermodel);         return usermodel;     });         app.filter('startfrom',function(){        return function(data,start){           if(data != null) {            //console.log("data fetched");            //console.log('data: %j',data);            return data.slice(start);            }           else if(data == null){ console.log("data null"); }        }     });      /************************dash-controller************************/      var st1;      var st2;     app.controller('dashctrl', function($rootscope,$scope,$http,$cookies,$localstorage,$filter,$routeparams,$location,$window){         console.log('inside dashctrl');         $scope.users = [];        $scope.pagesize=10;       // $scope.currentpage=1;        if($location.search().page!=undefined){             $scope.currentpage=$location.search().page;        }        else{            $scope.currentpage=1;        }         $scope.check = function(){             var mobilereg = /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;             var digitreg = /^\d+$/;             var dashno={                          user_name:  $cookies.get('user_name'),                          token: $cookies.get('token')                      };                     if ($location.search().mobile_no==undefined) {                         console.log("$scope.dash.mobile")                         dashno.mobile_no=$scope.dash.mobile;                      } else {                         dashno.mobile_no=$location.search().mobile_no;//........                     }                      $scope.mobile_no = dashno.mobile_no;                     if(mobilereg.test($scope.mobile_no) && digitreg.test($scope.mobile_no)){                        $scope.dashform1hide = true;                        /*$scope.showdetails=false;*/                           $http.post('http://-------------/get_user_info', dashno).                         success(function(data,status,header,config){                         console.log("data: %j", data);                         if(data.success == 1){                             console.log("check fn ...............");                             if ($location.search().mobile_no==undefined) {                             $location.url('?mobile_no=' + $scope.dash.mobile );                              }                             $scope.showdetails=true;                              $scope.email_id = data.email_id;                             $scope.user_name = data.user_name;                             $scope.default_vpa = data.default_vpa;                             $scope.def_vpa_name = data.def_vpa_name;                             //$scope.ab;                             /*if($location.search().page!=undefined){                                  $scope.ab=true;   //fetchedtrans();                                  //$scope.currentpage=$location.search().page;                                  console.log('inside check $scope.currentpage: ', $scope.currentpage);                                  fetchedtrans();                                  //$scope.pagechanged($location.search().page);                             }                             else{                                 $scope.ab=false;                                 fetchedtrans();                             }*/                             fetchedtrans();                          }else if (data.success == -1) {                             //mobile no. not exist                             document.getelementbyid("msgfornx").innerhtml="mobile no. not exist";                         }                          else{                              console.log('check in else')                          }                         }).                         error(function(data,status,header,config){                             console.log("data"+data);                             console.log('status'+status);                             console.log('header'+header);                             console.log('config: %j',config);                             alert("something wrong");                         })                     };        }                 console.log('$location.search().mobile_no'+$location.search().mobile_no);             //console.log('$scope.mobile_no'+$rootscope.mobile_no);              //console.log("$scope.email_id"+$scope.email_id);              if($location.search().mobile_no!=undefined){                      $scope.check();             }             else{                 console.log("check not called");             }         $scope.mobregex=/^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/;       $scope.regdigit = /^\d+$/;          $scope.pagechanged = function(currentpage) {             console.log('$scope.currentpage: '+ $scope.currentpage);             console.log('currentpage: ', currentpage);              st1=$location.search().page;             console.log("$location.search().page beore updating url"+st1);               /*if($scope.ab){                 *//*currentpage=$location.search().page;*//*                 //$scope.currentpage=$location.search().page;                 $scope.currentpage=currentpage;                 console.log('currentpage in if: ', currentpage);               } else {                 $scope.currentpage=currentpage;                 console.log('currentpage in else: ', currentpage);             }*/            // st1=$location.path('/add').search({ mobile_no: $location.search().mobile_no, page: $scope.currentpage});              $scope.currentpage=currentpage;              $location.path('/add').search({ mobile_no: $location.search().mobile_no, page: $scope.currentpage});            // st2=$location.path('/add').search({ mobile_no: $location.search().mobile_no, page: $scope.currentpage});             st2=$location.search().page;             console.log("$location.search().page after updating url"+st2);                 console.log("$scope.users[($location.search().page-1)*10]"+$scope.users[($location.search().page-1)*10]);              if($scope.users[($location.search().page-1)*10]==undefined){                fetchedtrans();             }             else{                console.log("data came aleady");             }         };     });          /*******************end of dash-controller***********************/    app.run(function($rootscope,$location,$scope,usermodel,$window,$routeparams,$route){         $rootscope.$on('$routeupdate', function(event, next, current){             console.log("event: %j",event);             console.log("next: %j",next);             console.log('current: %j',current);               //$rootscope.previouslocation=st2;             console.log("$rootscope.previouslocation"+$rootscope.previouslocation);             /*if($rootscope.previouslocation == $location.path()) {                  console.log("back button pressed");             }              $rootscope.previouslocation = $rootscope.actuallocation;*/             if($location.search().mobile_no!=undefined && $location.search().page!=undefined){                          //if($window.history.back()){                         console.log("st1 "+st1);                         console.log("st2 "+st2);                         if(history.length>=1){                             st2=st1;                             $rootscope.pagechanged(st2); //showing error @ line                             $scope.$apply();                         }                        }             $rootscope.actuallocation = $location.path();              console.log('$rootscope.actuallocation'+$rootscope.actuallocation);             //$location.url('/dashboard').replace(undefined);             console.log('$location.url()'+$location.url());             console.log('$location.url().replace()'+$location.url().replace());            if (next.$$route.authenticated) {               console.log('usermodel.getauthstatus app.run if 1: %j',usermodel.getauthstatus());               if (!usermodel.getauthstatus()) {                 console.log("getauthstatus ",!usermodel.getauthstatus);                 console.log('usermodel.getauthstatus app.run if 1(1)'+usermodel.getauthstatus());                 $location.path('/');             }           }            if (next.$$route.originalpath =='/') {               console.log("next.$$route.originalpath  "+next.$$route.originalpath);               /*console.log("current.$$route.originalpath  "+$route.current.$$route.originalpath);*/                 if (usermodel.getauthstatus()) {                                 console.log("current "+current);                                 console.log("next "+next);                                 next.$$route.originalpath = '/dashboard';                                 $location.path(next.$$route.originalpath);                                 $location.url('/dashboard').replace(undefined);                                 //current.$$route.originalpath = next.$$route.originalpath;                                 //current.$$route.originalpath = '';                             } else {                              }             }         });      }); 

if understand question correctly, 1 way think of attach pagechanged() function $rootscope in controller

angular.module('app')     .controller('appcontroller', function ($rootscope){         $rootscope.pagechanged = function(){             <your_code>          }   }); 

then inject $rootscope app.run block in app.js , call pagechanged function $rootscope.pagechanged()

but unless share code snippet, it's hard know if asking or if causing error you've mentioned.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -