我有一个离子应用程序,与离子服务工作正常,但当我在Android设备上运行时,我得到当我点击登录表单时出现此错误
“作为第一个参数需要的函数!在file:///android_asset/www/lib/ionic/js/ionic.bundle.min.js”,
调试后我发现更改状态时会触发此错误,
$state.go('map'),
当我评论这一行
//$state.go('map')
,没有发生错误,用户已成功登录。但疯狂的事情是其他州工作正常,只有登录状态不起作用。
控制器:
.controller('HomeCtrl', function ($rootScope, $http, $scope, $state, $stateParams, $ionicPopup, $ionicLoading, $ionicHistory, $timeout, $cordovaOauth, $location, localStorageService, ProfileService) {
$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
$scope.updateLocation()
$rootScope.googleProfile = null;
$rootScope.facebookProfile = null;
// //console.log('State changed: ', toState)
$scope.facebook = function() {
$cordovaOauth.facebook("282650518775396", ["email", "public_profile"], {redirect_uri: "http://localhost/callback"}).then(function(result){
$scope.displayData(result.access_token);
}, function(error){
alert("Error: " + error);
});
}
$scope.displayData = function(access_token) {
$http.get("https://graph.facebook.com/v2.2/me", {params: {access_token: access_token, fields: "name,email,id,gender,location,picture", format: "json" }}).then(function(result) {
// alert("name: " + result.data.name);
console.log("facebook profile: " + JSON.stringify(result.data));
ProfileService.facebook(result.data, function(response) {
if (! response) {
// //console.log('login ou mot de passe incorrect !')
localStorageService.set('facebookProfile', result.data);
$timeout(function() {
$state.go('register');
}, 100);
} else {
$timeout(function () {
// console.log('redirect to categories !')
$ionicLoading.hide()
$ionicHistory.clearCache()
$ionicHistory.clearHistory()
$ionicHistory.nextViewOptions({
disableBack: true,
historyRoot: true
})
if (ProfileService.profile().type == 1)
$state.go('app.map')
else
$state.go('app.categories')
}, 100)
}
});
}, function(error) {
alert("Error: " + error);
});
}
if ($location.path() == '/home') {
if (auth = ProfileService.isAuth()) {
$ionicLoading.show({
template: 'Auto connexion...'
})
$timeout(function () {
ProfileService.get(ProfileService.profile().id, function (response) {
localStorageService.set('profile', response)
$rootScope.profile = response;
$ionicHistory.clearCache()
$ionicHistory.clearHistory()
$ionicHistory.nextViewOptions({
disableBack: true,
historyRoot: true
})
$ionicLoading.hide()
if (response.type == 1)
$state.go('app.map')
else
$state.go('app.categories')
})
}, 100)
}
}
})
$scope.user = {}
$scope.updateLocation = function() {
navigator.geolocation.getCurrentPosition(function (pos) {
$scope.user.lat = pos.coords.latitude
$scope.user.lng = pos.coords.longitude
if (auth = ProfileService.isAuth()) {
ProfileService.updateGeo({
lat: pos.coords.latitude,
lng: pos.coords.longitude,
user_id: auth.id
}, function(response) {
console.log(response)
})
}
console.log('location updated', $scope.user)
}, function (error) {
console.log('Unable to get location: ' + error.message)
})
}
// login
$scope.login = function () {
if ($scope.user.mail && $scope.user.password) {
// //console.log($scope.user)
$ionicLoading.show({
template: 'Chargement...'
})
ProfileService.Auth($scope.user, function (response) {
console.log(response)
if (! response) {
// //console.log('login ou mot de passe incorrect !')
$ionicPopup.alert({
template: 'login ou mot de passe incorrect !',
title: 'Erreur'
})
$ionicLoading.hide()
} else {
$timeout(function () {
// //console.log('redirect to categories !')
$ionicLoading.hide()
$ionicHistory.clearCache()
$ionicHistory.clearHistory()
$ionicHistory.nextViewOptions({
disableBack: true,
historyRoot: true
})
if (ProfileService.profile().type == 1) {
// $state.go('app.map')
$location.url('app/map')
console.log('redirect to map !')
}
else {
// $state.go('app.categories')
$location.url('app/categories')
console.log('redirect to categories !')
}
}, 100)
}
})
} else {
$ionicPopup.alert({
title: 'Champs obligatoires',
template: 'Tous les champs sont obligatoires!'
})
}
}
// end login
})
.controller('CategoriesCtrl', function ($ionicNativeTransitions, $cordovaGeolocation, $stateParams, $rootScope, $scope, $state, $log, $location, $timeout, $ionicLoading, $ionicPopup, localStorageService, ProfileService, CategoryService, requestService, paymentService, $ionicHistory) {
$scope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
// //console.log('State changed: ', toState)
$scope.data = {
lat: '',
lng: '',
destination_lat: '',
destination_lng: '',
username: ProfileService.profile().name,
user_id: ProfileService.profile().id,
forfait: $stateParams.forfait,
}
if ($location.path() == '/app/categories') {
$ionicLoading.show({
template: 'Chargement...'
})
if (! ProfileService.isAuth()) {
$state.go('home')
}
$scope.getProfile()
$scope.getCategories(function () {
$scope.categories = $.map($scope.categories, function(value, index) {
return [value];
});
$timeout(function () {
$ionicLoading.hide()
}, 1000)
})
} else if ($location.path() == '/app/subcategories/'+$stateParams.category) {
$scope.getSubCategories(function() {
$scope.subcategories = $.map($scope.subcategories, function(value, index) {
return [value];
});
})
} else if ($location.path() == '/app/forfaits/'+$stateParams.subcategory) {
$scope.getForfaits(function() {
$scope.forfaits = $.map($scope.forfaits, function(value, index) {
return [value];
});
})
} else if ($location.path() == '/app/detail_forfait/'+$stateParams.forfait) {
$scope.getCurrentPosition()
$scope.getDetailForfait(function() {
$scope.options = $.map($scope.options, function(value, index) {
return [value];
});
})
}
})
$scope.getProfile = function () {
ProfileService.get(ProfileService.profile().id, function (response) {
$rootScope.profile = response
})
}
// $scope.loadCategory = function (id) {
// $ionicNativeTransitions.stateGo('app.category', {id: id}, {
// 'type': 'slide',
// 'direction': 'up', // 'left|right|up|down', default 'left' (which is like 'next')
// 'duration': 1500, // in milliseconds (ms), default 400
// })
// }
$scope.location = {
formatted_address: ""
};$scope.destinationlocation = {
formatted_address: ""
};
/*$scope.address = {};*/
$scope.searchPosition = function(location, type) {
type = type || null;
console.log(type)
console.log(location)
var geo = new google.maps.Geocoder;
geo.geocode({'address':location},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var myLatLng = results[0].geometry.location;
if (type == 'destination') {
$scope.data.destination_lat = results[0].geometry.location.lat();
$scope.data.destination_lng = results[0].geometry.location.lng();
$scope.map.setCenter(myLatLng)
$scope.destmarker = new google.maps.Marker({
position: myLatLng,
map: $scope.map
})
} else {
$scope.data.lat = results[0].geometry.location.lat();
$scope.data.lng = results[0].geometry.location.lng();
$scope.map.setCenter(myLatLng)
$scope.marker.setPosition(myLatLng)
}
if ($scope.data.lat && $scope.data.lng && $scope.data.destination_lat && $scope.data.destination_lng) {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
$scope.map.setCenter(myLatLng)
var request = {
origin: $scope.data.lat+','+$scope.data.lng,
destination: $scope.data.destination_lat+','+$scope.data.destination_lng,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
// Display the distance:
$scope.distance = response.routes[0].legs[0].distance.value;
$scope.distance = $scope.distance >= 1000 ? $scope.distance/1000 + " Km" : $scope.request.distance + "M"
// Display the duration:
$scope.duration = moment.duration(response.routes[0].legs[0].duration.value,"seconds").format("h [hrs], m [min]");
directionsDisplay.setDirections(response);
directionsDisplay.setMap($scope.map);
}
});
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
$scope.initMap = function () {
console.log($scope.data)
var myLatlng = new google.maps.LatLng(43.07493, -89.381388)
var mapOptions = {
center: myLatlng,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'),
mapOptions)
if ($scope.data.lat && $scope.data.lng) {
var myLatlng = new google.maps.LatLng($scope.data.lat , $scope.data.lng)
$.get('http://maps.googleapis.com/maps/api/geocode/json?latlng='+$scope.data.lat+','+$scope.data.lng+'&sensor=false', function(data) {
if (data.status == 'OK') {
$scope.location.formatted_address = data.results[0].formatted_address;
};
});
map.setCenter(myLatlng)
$scope.marker = new google.maps.Marker({
position: myLatlng,
map: map
})
}
$scope.map = map
}
$scope.disable_after_send = false
$scope.makeRequest = function () {
var error = false;
if ($scope.category == 520) {
if ($scope.data.destination_lat == '' && $scope.data.destination_lng == '') {
error = true;
$ionicPopup.alert({
title: 'Info',
template: 'Vous devez choisir une destination'
});
}
}
if ( ! error) {
$ionicPopup.show({
title: 'Confirmation',
template: "Vous allez être débiter de "+$scope.forfait.price+" €",
buttons: [
{
text: 'Annuler',
},
{
text: 'Confirmer',
type: 'button-positive',
onTap: function(e) {
$ionicLoading.show({
template: 'Chargement...'
});
$scope.data.price = $scope.forfait.price;
$scope.data.options = JSON.stringify($scope.selected_options);
$scope.disable_after_send = true;
requestService.send($scope.data, function (response) {
console.log('response', response)
$scope.disable_after_send = false
if (response.send_to) {
$ionicPopup.alert({
template: "Votre demande à été envoyée - en attente d'un profesionnel",
title: 'Demande envoyée'
})
$ionicHistory.nextViewOptions({
disableBack: true
})
$state.go('app.request', { request: response.request_id })
} else {
$ionicLoading.hide();
$ionicPopup.alert({
template: "Aucun pro n'est disponible actuellement dans votre zone !",
title: "Demande non envoyée"
})
}
})
}
},
{
text: 'Changer carte de paiement',
type: 'button-assertive',
onTap: function(e) {
$state.go('app.card', { action: 'change'});
}
}
]
});
}
/*confirmPopup.then(function(res) {
if (res) {
$scope.data.price = $scope.forfait.price;
$scope.data.options = JSON.stringify($scope.selected_options);
$scope.disable_after_send = true;
requestService.send($scope.data, function (response) {
console.log('response', response)
$scope.disable_after_send = false
if (response.send_to) {
$ionicPopup.alert({
template: "Votre demande à été envoyée - en attente d'un profesionnel",
title: 'Demande envoyée'
})
$ionicHistory.nextViewOptions({
disableBack: true
})
$state.go('app.myrequests')
} else {
$ionicPopup.alert({
template: "Aucun pro n'est disponible actuellement dans votre zone !",
title: "Demande non envoyée"
})
}
})
}
})*/
}
$scope.getCurrentPosition = function() {
$ionicLoading.show({
template: 'localisation...'
})
var posOptions = {timeout: 10000, enableHighAccuracy: false}
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (pos) {
$scope.data.lat = pos.coords.latitude
$scope.data.lng = pos.coords.longitude
$scope.initMap()
$ionicLoading.hide()
}, function (err) {
$ionicLoading.hide()
})
}
$scope.subcategories = {}
$scope.getSubCategories = function (cb) {
CategoryService.subcats({
category_id: $stateParams.category
}, function(response) {
$scope.subcategories = response
})
cb();
}
$scope.forfaits = {}
$scope.getForfaits = function (cb) {
CategoryService.forfaits({
sub_category_id: $stateParams.subcategory
}, function(response) {
console.log(response)
$scope.forfaits = response
})
cb();
}
$scope.forfait = {}
$scope.options = {}
$scope.category = {}
$scope.selected_options = [];
$scope.toggleOption = function(index, id, selected) {
if (selected) {
$scope.selected_options.push(id);
$scope.forfait.price = parseInt($scope.forfait.price) + parseInt($scope.options[index].price);
} else {
var i = $scope.selected_options.indexOf(id);
if (i > -1) {
$scope.selected_options.splice(i, 1);
}
$scope.forfait.price = parseInt($scope.forfait.price) - parseInt($scope.options[index].price);
}
console.log($scope.selected_options);
}
$scope.getDetailForfait = function(cb) {
CategoryService.detail_forfait({
forfait: $stateParams.forfait
}, function(response) {
console.log(response)
$scope.forfait = response.forfait;
$scope.options = response.options;
$scope.category = response.category_id;
})
cb();
}
$scope.categories = [];
$scope.getCategories = function (cb) {
CategoryService.all(function (data) {
$scope.categories = data;
/*angular.forEach(data, function(cat) {
CategoryService.check({
user_id: $rootScope.profile.id,
category_id: cat.id
}, function (response) {
$ionicLoading.hide()
if (response.exist) {
// //console.log("yes")
cat.exist = true
} else {
cat.exist = false
}
$scope.categories.push(cat)
// //console.log(cat)
})
})*/
//console.log($scope.categories)
cb()
})
}
})
个人资料服务
.service('ProfileService', function ($rootScope, $log, $timeout, $ionicPopup, localStorageService, webService) {
this.profile = function () {
return localStorageService.get('profile')
}
this.isAuth = function () {
return this.profile()
}
this.online = function (info, cb) {
$.ajax({
url: webService.buildUrl('online'),
method: 'POST',
data: info,
dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
};
this.offline = function (info, cb) {
$.ajax({
url: webService.buildUrl('offline'),
method: 'POST',
data: info,
dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
};
this.updateGeo = function (info, cb) {
$.ajax({
url: webService.buildUrl('update_geo'),
method: 'POST',
data: info,
dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.updateProfilePicture = function (info, cb) {
$.ajax({
url: webService.buildUrl('update_profile_picture'),
method: 'POST',
data: info,
// dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.resetPassword = function (info, cb) {
$.ajax({
url: webService.buildUrl('reset_password'),
data: info,
dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.get = function (id, cb) {
$.ajax({
url: webService.buildUrl('get_user'),
data: {
user_id: id
},
dataType: 'json',
success: function (response) {
cb(response)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.validateEmail = function (email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return re.test(email)
}
this.Auth = function (user, cb) {
$.ajax({
url: webService.buildUrl('login'),
method: 'POST',
dataType: 'json',
data: user,
success: function (data) {
$log.debug(data)
if (! data.user) {
// $log.debug('not logged in')
cb(false)
} else {
// $log.debug('profile in localStorageService')
localStorageService.set('profile', data.user)
// $rootScope.unbind = localStorageService.bind($rootScope, 'profile')
cb(true)
}
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus + ':' + errorThrown)
}
})
}
this.logout = function (cb) {
localStorageService.remove('profile')
cb()
}
this.status = function (info, cb) {
$.ajax({
url: webService.buildUrl('state'),
method: 'POST',
data: info,
success: function (data) {
cb(data)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.type = function (info, cb) {
console.log(info)
$.ajax({
url: webService.buildUrl('type'),
method: 'POST',
dataType: 'json',
data: info,
success: function (data) {
cb(data)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.update = function (user, cb) {
$.ajax({
url: webService.buildUrl('update'),
method: 'POST',
dataType: 'json',
data: user,
success: function (data) {
cb(data)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.updatePassword = function (info, cb) {
$.ajax({
url: webService.buildUrl('update_password'),
method: 'POST',
dataType: 'json',
data: info,
success: function (data) {
cb(data)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.register = function (user, cb) {
$.ajax({
url: webService.buildUrl('register'),
method: 'POST',
dataType: 'json',
data: user,
success: function (data) {
cb(data)
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
this.switchMode = function (mode) {
// $log.debug('switchMode: ' + mode)
// $log.debug('profile id: ' + this.profile().id)
$.ajax({
url: webService.buildUrl('changemode'),
method: 'POST',
dataType: 'json',
data: {
user_id: this.profile().id,
type: mode
},
success: function (data) {
// $log.debug(data)
if (data) {
localStorageService.set('profile', data)
}
},
error: function (jqXHR, textStatus, errorThrown) {
$log.error(textStatus)
}
})
}
})
Cordova CLI:6.1.1 Gulp版本:CLI版本3.9.1 Gulp local:本地版本3.9.1 离子框架版本:1.3.1 离子CLI版本:1.7.16 Ionic App Lib版本:0.7.3 操作系统:节点版本:v4.4.4
我正在使用: 离子束1.3.1 angularjs v1.5.8