【问题标题】:Angular refresh after $scope change$scope 更改后的角度刷新
【发布时间】:2015-04-30 06:32:12
【问题描述】:

在我的应用程序中,我使用谷歌地图 API v3 来在地图上显示信息窗口。信息窗口充满了$scope 值。地图加载后,$scope 因事件发生变化,但我的信息窗口内容还是一样。

如何让它刷新?

我已经尝试在事件回调函数结束时调用$scope.$apply(),但没有成功。

Controller.js

this.fillingMethod = function(){
            info = MyService.getInformation();
            $scope.fillingInfo = info;
}
this.changeAnInfo = function(){
            var info = $scope.fillingInformation;
            var info = MyService.changeInfo(info);
            $scope.fillingInformation = info;
            $scope.$apply();
            }//Google InfoWindow is not refreshed.

View.html

    <!-- ng-controller is defined and refers to Controller -->
    <map id="map-canvas" center="current-location" zoom="15" ng-init="map.fillingMethod()">
        <marker id='test' position="current-location" animation="BOUNCE"></marker>
            <info-window id="marker-info" class="gmarker">
                <div ng-non-bindable="">
                     <div id="siteNotice"></div>
                         <h1 id="firstHeading" class="firstHeading">{{fillingInformation}}</h1>
                     </div>
                </div>
            </info-window>
    </map>
<button ng-click="changeAnInfo()" />

【问题讨论】:

  • @Noémie O'connor 更新答案,看看它是否适合你:)

标签: javascript angularjs google-maps ionic-framework


【解决方案1】:

angular-google-maps 看看这个。这是角度的谷歌地图。您将使用自定义指令。只需将您的$scope 放入其中,它就会自动更新,就像您将范围放入html tags 一样。

查看 API,使用 window 指令或 windows

编辑:你的代码中有 2 个奇怪的东西

  • 首先,为什么会有ng-non-bindable 它会阻止angular 编译里面的东西。 尝试将其删除?
  • 您使用的库看起来不像angular-google-mapsangular-ui。如果是的话。然后你将有自定义指令/属性。要进行更新,只需将信息放入标签内即可。

<map id="map-canvas" center="current-location" zoom="15" ng-init="map.fillingMethod()">
    <marker id='test' position="current-location" animation="BOUNCE"></marker>
        <info-window id="marker-info" class="gmarker">
            <div >
                 <div id="siteNotice"></div>
                     <h1 id="firstHeading" class="firstHeading">{{fillingInformation[0]}}</h1>
                 </div>
            </div>
        </info-window>
</map>

【讨论】:

  • 我已经在使用谷歌地图来获取角度。也许我无法正确使用它,因为在 $scope 更新后我的 InfoWindow 没有刷新。
  • @Noémie O'connor 到目前为止,我并没有太多使用它,但是在通过 API 获取一些标记的测试中使用它时它确实对我有用。它对我来说很好。让我看看你的代码。在那里发现了一些奇怪的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
相关资源
最近更新 更多