私のアプリには緯度と経度の属性を持つイベントコンセプトがあります。クライアント側では、navigator.geolocationを使用して各イベントへの現在の距離を計算し、ビューに表示したいと考えています。ngResourceオブジェクトにasync 'インスタンスメソッド'を作成
以下は私が今までに持っているものです。実行されますが、無限ループに陥っています(さらにエラーが発生します)。
factory('ys$currentPosition', ['$q', '$window', function($q, $window){
'use strict';
return {
position: function(){
var deferred = $q.defer();
if ($window.navigator.geolocation) {
$window.navigator.geolocation.getCurrentPosition(
function (position) {
deferred.resolve(position);
},
function (err) {
deferred.reject(err);
}
);
} else {
deferred.reject('Geolocation not supported.');
}
return deferred.promise;
}
};
}]).
factory('Event', ['$resource', 'ys$currentPosition', function($resource, ys$currentPosition){
'use strict';
var Event = $resource($server_hostname + '/api/v4/organizations/' + $org_id + '/events/:id', {}, {});
Event.prototype.distance = function(){
var this_event = this;
return ys$currentPosition.position().then(function(position){
return getDistanceFromLatLonInMi(
position.coords.latitude,
position.coords.longitude,
this_event.latitude,
this_event.longitude
)
});
}
return Event;
}]).
View (in HAML):
%ys-index-row-right{"ng-if" => "item.distance()"}
%i.fa.fa-map-marker
%span
{{ item.distance() }} mi
ここでそのエラーがあります:
angular-0195028….js?body=1:69 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"item.distance()","newVal":{},"oldVal":{}}],[{"msg":"item.distance()","newVal":{},"oldVal":"..."}],[{"msg":"item.distance()","newVal":{},"oldVal":"..."}],[{"msg":"item.distance()","newVal":{},"oldVal":"..."}],[{"msg":"item.distance()","newVal":{},"oldVal":"..."}]]
Plunker:
https://plnkr.co/edit/zk5ETxCzvzKYAJOlUPPA?p=preview
常に変化しています。 – amanuel2
プランナーを作成 –
@DrJonesプランナーが追加されました - ありがとう! – jsharpe