【问题标题】:Capturing changes to model using angular-google-places-autocomplete使用 angular-google-places-autocomplete 捕获对模型的更改
【发布时间】:2014-12-01 17:55:23
【问题描述】:

我将 angular-google-places-autocomplete (https://github.com/kuhnza/angular-google-places-autocomplete) 与 Ionic 一起使用,但在使用此指令时捕获所选选项时遇到问题。

我的指令是这样设置的:

<!-- template -->
<input type="text" placeholder="Place search" g-places-autocomplete ng-model="locationSearchResult"/>

<h5>Result</h5>
<pre ng-bind="locationSearchResult | json"></pre>

我的控制器代码设置为监视 locationSearchResult 模型的更改,如果确实发生更改,则将新位置保存到本地存储:

// Controller
$scope.locationSearchResult = {};    

$scope.$watch('locationSearchResult', function(newVal, oldVal) {
  if (angular.equals(newVal, oldVal)) { return; }
  $scope.$storage.loc = newVal;
  $state.go('new-page');

});

使用自动完成时,它似乎按预期工作 - 我得到一个预测列表,然后从预测列表中选择一个预测会更新文本输入,其中包含所选地点的名称和所选地点的 JSON 数据显示在结果标题下。但是,控制器中的 $scope.$watch 似乎没有接收到更改。

因此,我似乎无法捕获搜索结果数据并对其进行任何操作 - 例如将其添加到用户会话中。

也许我只是以错误的方式处理它(尽管我对 ngAutocomplete 使用了相同的方法并且它工作正常)。

【问题讨论】:

    标签: autocomplete angularjs-directive google-places-api ionic-framework


    【解决方案1】:

    使用在您的控制器中发出的事件。

    $scope.$on('g-places-autocomplete:select', function (event, param) {
      console.log(event);
      console.log(param);
    });
    

    【讨论】:

    猜你喜欢
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多