离子范围不会更新内部功能

时间:2016-03-17 15:39:13

标签: angularjs cordova ionic-framework

我试图使用cordova地理定位插件获取我的位置,我的控制器看起来像:

.controller('HomeCtrl', function($scope,$cordovaGeolocation,$ionicLoading) {

    $scope.location="location";
    $ionicLoading.show({
        template: '<ion-spinner icon="bubbles"></ion-spinner><br/>Acquiring location!'
    });

    var posOptions = {
        enableHighAccuracy: true,
        timeout: 15000,
        maximumAge: 0
    };

    $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
        var lat  = position.coords.latitude;
        var lng = position.coords.longitude;

        var geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(lat, lng);
        var request = {
          latLng: latlng
        };
        geocoder.geocode(request, function(data, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (data[3] != null) {

              $scope.location=data[3].formatted_address;
              console.log($scope.location);
            } else {
              console.log("No address available");
            }

          }

        })

        $ionicLoading.hide();           

    }, function(err) {
        $ionicLoading.hide();
        console.log(err);
    });
})

在视图中,$scope.location的值仍为&#34; location&#34;虽然它已经更新了地理编码器功能。但是当我在控制台日志中显示它时,它会显示我当前的城市。任何人都可以帮助我吗?

这是我的HTML

  <ion-content scroll="true" ng-controller="HomeCtrl">
    <h3>{{location}}</h3>
  </ion-content>

1 个答案:

答案 0 :(得分:0)

尝试在.directive('encodedInput', function ($window) { return { restrict: 'A', require: '?ngModel', link: function (scope, element, attrs, ngModel) { if (!ngModel) return; // do nothing if no ng-model // Specify how UI should be updated ngModel.$render = function () { element.html($window.he.decode(ngModel.$modelValue || '')); }; var inputString = ''; // Listen for change events to enable binding element.on('blur keyup change', function () { //Check wether the string has changed. So the & will not be extra encoded. if (inputString != ngModel.$viewValue) {//Combined without logging console.log(length); length = inputString.length; ngModel.$setViewValue($window.he.encode(ngModel.$viewValue, { 'useNamedReferences': true })); } inputString = ngModel.$viewValue; }); function read(string) { } } }; } 之后添加$scope.$apply(); ...应强制更新范围。