Я в ситуации, когда должен передавать переменные LntLng службе, где запрос $ http.get выполняется с использованием этих переменных широты и долготы в качестве параметров URL.
Я пробовал некоторые способы, используя localstorage, эта методология localstorage была в какой-то мере полезной, но она может устанавливать переменные только один раз, а не обновляться до новых значений, если пользователь меняет свое местоположение на какое-то другое место. Мне нужно каждый раз обновлять переменную с новой ваулей пользователь изменение там адрес.
.factory('someFactory', function($http) {
return {
get: function(result) {
var latitude='value from directive variable';
var longitude='value from directive variable';
$http.get('http://www.someapi.com/?longitude=' + longitude + '&latitude='+ latitude + '&customer_number=00000')
.success(function(result) {
});
}
}
});
Директива:
popupPromise.then(function(el) {
var searchInputElement = angular.element(el.element.find('input'));
scope.selectPlace = function(place) {
ngModel.$setViewValue(place);
map.setCenter(place.geometry.location); // from here i will get new updated place object with variable i need..My problem is to access the 'place' object in service .
ngModel.$render();
console.log('rendering');
el.element.css('display', 'none');
$ionicBackdrop.release();
};
};
ПРИМЕЧАНИЕ. Я буду получать значение из директивы только после обновления объекта "место" и отправлю это значение в службу после полной загрузки директивы.
Любые предложения, пожалуйста.
Вы можете достичь этого легко через $watch
или $observe
(если значение места исходит от атрибутов директивы) в директиве.
//Обслуживание
.factory('someFactory', function($http) {
return {
get: function(lat, lng) {
return $http.get('http://www.someapi.com/?longitude=' + lng + '&latitude='+ lat + '&customer_number=00000');
}
}
});
//Директива
directives.directive('someDirective', ['someFactory', function(someFactory) {
return {
link: function($scope, element, attr) {
function responseHandler() {
// Your code on response from the API
ngModel.$setViewValue(place);
map.setCenter(place.geometry.location); // from here i will get new updated place object with variable i need..My problem is to access the 'place' object in service .
ngModel.$render();
console.log('rendering');
el.element.css('display', 'none');
$ionicBackdrop.release();
}
// Look for the change in place entered by user
attr.$observe('place', function(newValue) {
if (newValue) {
someFactory.get(newValue.lat, newValue.lng).then(responseHandler);
}
});
// OR //
// Watch for any scope variable (if you have place object as stored in Scope)
$scope.$watch('place', function(newValue) {
if (newValue) {
someFactory.get(newValue.lat, newValue.lng).then(responseHandler);
}
});
},
};
}]);