使用服务与用户位置更新域 [英] Update fields with user position using a service
问题描述
我想,当我收到可以将GeoPosition对于给用户更新某些字段。
I would like to update some fields when I receive a geoposition for a give user.
直到知道我有以下的code:
Until know I have the following code:
首先,我收到了错误:未知提供商:positionProvider< - 位置
(仅适用于的jsfiddle)。我没有得到我的真正的网站这个错误。
Firstly, I get a Error: Unknown provider: positionProvider <- position
(only on jsfiddle). I do not get this error on my real site.
问题是,当我得到了我的位置更新位置
对象的服务,但它不会在HTML视图更新。
The problem is that when I get the position I update the position
object in the service but it does not update in the HTML view.
有什么建议?
推荐答案
试试这个。 小提琴
var test = angular.module('myApp', []);
var services = angular.module('myApp.services', []);
services.factory('position', ['$http', function ($http) { ...
应
var test = angular.module('myApp', []);
test.factory('position', ['$http', function ($http) { ...
您应该更新控制器code,因为这,要使用一个回调函数和$适用于申请设置范围的值。
You should update the controller code as this, to use a callback function and $apply to apply the value set to the scope.
position.getPosition(function (p) {
$scope.$apply(function () {
$scope.position.latitude = p.coords.latitude;
$scope.position.longitude = p.coords.longitude;
$scope.position.accuracy = p.coords.accuracy;
});
});
这篇关于使用服务与用户位置更新域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!