我的应用程序使用 ionic 时遇到问题。在我的 Service.js 中,我有一个检索我的位置的函数,在我的 controller.js 中,我有一个函数,当我在我的 html 页面中按下一个按钮时,将用户发送到另一个页面。因此,当我按下一个按钮时,在应用程序将我发送到另一个页面之前,我希望在我的 service.js(地理定位)中启动该功能,并且仅本地理定位成功时才发送给我。
我该怎么做?
我想我可以在 service.js 上保存一个变量。如果函数进入 onSuccess,我保存变量 = 1,如果函数进入错误,我保存变量 = 0。但我不知道如何在我的 Controller 上使用这个变量...
服务.js :
.factory('position', function() {
return{
positioning : function(){
var geocoder;
geocoder = new google.maps.Geocoder();
initialise = function() {
navigator.geolocation.getCurrentPosition(onSuccess, onError, { enableHighAccuracy : true, timeout : 5000, maximumAge : 0});
function onSuccess(position){
myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
}
function onError(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
cordova.plugins.settings.openSetting("open", function(){console.log("opened settings")},function(){console.log("failed to open nfc settings")});
}
}
google.maps.event.addDomListener(window, 'load', initialise());
Controller.js:
.controller('MainCtrl', function($scope,$state, position) {
$scope.mappa=function(){
$state.go('tab.mappa');
position.positioning();
}
$scope.dettagli=function(){
$state.go('tab.dettagli');
position.positioning();
}
$scope.strada=function(){
$state.go('tab.strada');
position.positioning();
}
$scope.segnalazioni=function(){
$state.go('tab.segnalazioni');
position.positioning();
}
$scope.SOS=function(){
$state.go('tab.sos');
position.positioning();
}
html 页面:
<ion-view view-title="Home" ng-controller="MainCtrl">
<ion-content class="padding">
<div >
<!-- LA MIA POSIZIONE -->
<div class="max-width width-forty">
<button class="button button-full button-royal" ng-click="mappa()">
<table>
<tr><td><i class="fa fa-map fa-hx fa-inverse"></i></td></tr>
<tr><td class="button-text-map-sos">La mia posizione</td></tr>
</table>
</button>
</div>
.........
..........
.........
最佳答案
在您使用 ui-router 时的配置模块中,您可以使用返回 promise 的服务检查位置,如果有位置则查看 map ,否则显示错误。
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider.state('home', {
url: '/',
templateUrl: 'page1.html',
controller: 'page1Ctrl'
})
.state('map-view', {
url: '/map-view/:forceBad',
templateUrl: 'page2.html',
controller: 'page2Ctrl',
resolve: {
location: function(GeoLocation, $q, $stateParams) {
return GeoLocation.getLocation($stateParams.forceBad)
.then(function(_response) {
return _response;
}, function(_error) {
alert('error getting location')
return $q.reject("no location found");
});
}
}
});
})
在示例 Controller 中,我们传递了一个标志来强制一个坏的或好的位置,这样你就可以在应用程序路由中看到这两个路径
.controller('page1Ctrl', function($scope, $state) {
// click a button either force bad result or get good result
$scope.clicked = function(_forceBad) {
alert('clicked with forceBad ' + _forceBad);
$state.go('map-view', {
forceBad: _forceBad
});
}
})
在服务中,当 forceBad
设置为 true
时,我们返回错误
.service('GeoLocation', function($state, $q) {
return {
/**
* when passed true, the service will not return a location
*/
getLocation: function(_forceBad) {
var deferred = $q.defer();
setTimeout(function() {
if (_forceBad === "true") {
return deferred.reject({
type: 'error',
});
}
return deferred.resolve({
type: 'location',
lat: 100,
lng: -100
})
}, 200);
return deferred.promise;
}
}
})
关于javascript - 使用来自 Controller 的服务变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438090/