如何调用声明到控制器中的函数,并需要将其调用到app.run block中?



我已经在控制器中编写了一个pageChanged()函数。我必须将这个函数调用到 app.run 块中。我提到参数为$rootScope、$scope等。 它给了我错误作为类型错误。 当我尝试在 app.run 块中实现浏览器后退按钮的逻辑时,它给了我错误。 错误行在我的代码中通过注释提到。 我的代码在这里:

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 is some 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 is 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. does 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 at this 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 {
}
}
});
});

如果我正确理解您的问题,我能想到的一种方法是您将 pageChanged(( 函数附加到控制器中的$rootScope,如下所示

angular.module('app')
.controller('AppController', function ($rootScope){
$rootScope.pageChanged = function(){
<your_code> 
}  
});

然后将$rootScope注入到 app 中的 app.run 块中.js并将 pageChanged 函数调用为 $rootScope.pageChanged((

但是,除非您共享代码片段,否则很难知道这是否是您所要求的,或者这是否是导致您提到的错误的原因。

最新更新