简体   繁体   English

角度控制器指令范围

[英]Angular Controller Directive Scope

I've been struggling with the following problem for the last couple of hours and I have no clue how to solve it. 在过去的几个小时中,我一直在努力解决以下问题,我不知道如何解决。 I'm making an angular application that uses the angular-openlayers-directive directive. 我正在做一个使用angular-openlayers-directive指令的角度应用程序。 The $scope.center object contains the position that is initially viewed on the map. $ scope.center对象包含最初在地图上查看的位置。 The following piece of code are the first lines of the controller of the page that is viewing the map. 以下代码是查看地图的页面控制器的第一行。 This all works well. 这一切都很好。

angular.extend($scope, {
    center: {
        lat: 0,
        lon: 0,
        zoom: 1,
        autodiscover: true
    },
    layers: {
        normal: {
            type: 'Tile',
            source: {
                type: 'OSM'
            }
        }
    }
});

However, now I'm trying to update the center object with a location gotten from the google geocoding api. 但是,现在我正尝试使用从Google地理编码api获取的位置更新中心对象。 Clicking a row of the results of a google api calls the following function in the controller: 单击一行google api结果的结果会在控制器中调用以下函数:

$scope.gotoLocation = function(lat, lon) {
    $log.log("called");
    $scope.center = {
        lat: 50,
        lon: 50,
        zoom: 0
    }
}

In my console the string log "called" appears but the view of the map does not change. 在我的控制台中,出现了字符串“被叫”的日志,但是地图的视图没有改变。 Also when I print $scope.center in the function the original object that is set above is printed, while this object has changed to my current lat and lon because the autodiscover option is set to true. 同样,当我在函数中打印$ scope.center时,会打印上面设置的原始对象,而该对象已更改为我当前的经纬度,因为autodiscover选项设置为true。 I have checked this with the following code: 我已经用以下代码检查了它:

<openlayers ol-controls="controls" ol-center="center" ol-layers="layers" ol-markers="markers">
    <ol-marker ng-repeat="marker in markers" lat="marker.lat" lon="marker.lon"></ol-marker>
</openlayers>

<form>
    Latitude : <input type="number" step="any" ng-model="center.lat" />
    Longitude : <input type="number" step="any" ng-model="center.lon" />
    Zoom : <input type="number" step="any" ng-model="center.zoom" />
</form>

All 3 fields show my current lat and lon and also when I change a field the map position updates(!). 所有3个字段都显示我当前的经度和纬度,并且当我更改字段时,地图位置也会更新(!)。 I have no clue why changing the model from the template itself does work and changing it from the controller does not work. 我不知道为什么从模板本身更改模型确实起作用而从控制器更改模型却不起作用。 Can anyone help me with this? 谁能帮我这个?

Link to the directive: https://github.com/tombatossals/angular-openlayers-directive/blob/master/dist/angular-openlayers-directive.js 链接到指令: https : //github.com/tombatossals/angular-openlayers-directive/blob/master/dist/angular-openlayers-directive.js

It seems your autodiscover variable is erased. 看来您的自动发现变量已删除。 Does that have to do with auto updating? 这与自动更新有关吗?

Try changing your gotoLocation function to update the properties rather than overwriting the object: 尝试更改gotoLocation函数以更新属性,而不是覆盖对象:

$scope.gotoLocation = function(lat, lon) {
    $log.log("called");

    $scope.center.lat = 50;
    $scope.center.lon = 50;
    $scope.center.zoom = 0;

}

It had to do with different scopes. 它与不同的范围有关。 My map view was included using ng-view. 使用ng-view包含了我的地图视图。 My search bar was included using ng-include, so they had different scopes. 我的搜索栏是使用ng-include包含的,因此它们的作用域不同。 I solved it using a factory. 我用工厂解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM