简体   繁体   English

Angular.js:服务和ui.router

[英]Angularjs: Services and ui.router

I use an angular service for rendering a map and I injected it into my controller. 我使用角度服务来渲染地图,并将其注入到控制器中。 By using ui.router, when I go to map page for first time, the map renders successfully, but when routing to other page and come back to map page again, the map does not renders, and I should refresh the page for it. 通过使用ui.router,当我第一次进入地图页面时,地图会成功渲染,但是当路由到其他页面并再次返回到地图页面时,地图不会渲染,我应该为其刷新页面。 I used both factory and service for it, but still there is a problem! 我同时使用了factoryservice ,但是仍然有问题! Any idea? 任何想法? Here is my service and controller: 这是我的服务和控制器:

angular.module('app')
    .service('mapService', function () {
        var _map = new ol.Map({
            target: 'map-canvas',
            renderer: 'canvas'
        });
        this.map = function () {
            return _map;
        };
    }
    .controller("mapCtrl", ["$scope", "mapService", function($scope, mapService) {
        var map = mapService.map();
    }]);

You should not use service for DOM related changes and initialising map is DOM related action. 您不应将服务用于DOM相关的更改,并且初始化映射是DOM相关的操作。 instead, use directive for this. 而是使用指令。

See the below example, I have used open layer map and ui.router to show how you can do the same with directive. 参见下面的示例,我使用了开放层地图和ui.router来展示如何使用指令进行相同操作。

 var app = angular.module('app', ['ui.router']); app.run(function($templateCache) { var homeHtml = '<div>' + '<h4>Rome Page</h4>' + '<p> this is rome </p>' + '<div> <ol-map center="rome"></ol-map> </div>' + '</div>'; $templateCache.put('rome.html', homeHtml); var otherHtml = '<div>' + '<h4>Bern Page</h4>' + '<p> this is bern</p>' + '<div> <ol-map center="bern"></ol-map> </div>' + '</div>'; $templateCache.put('bern.html', otherHtml); }); app.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise("/rome"); $stateProvider.state('rome', { url: "/rome", templateUrl: "rome.html", controller: function($scope) { $scope.rome = ol.proj.fromLonLat([12.5, 41.9]); } }).state('bern', { url: "/bern", templateUrl: "bern.html", controller: function($scope) { $scope.bern = ol.proj.fromLonLat([7.4458, 46.95]); } }); }); app.controller('ctrl', function($scope) { }); app.directive('olMap', function() { return { restrict: 'E', scope: { center: '=' }, link: function(scope, ele, attr) { scope.map = new ol.Map({ view: new ol.View({ center: scope.center, zoom: 6 }), layers: [ new ol.layer.Tile({ source: new ol.source.MapQuest({ layer: 'osm' }) }) ], target: ele[0] }); } }; }); 
 ol-map { width: 500px; height: 300px; display: block; } ul { padding: 0px; } ul li { padding: 0; margin: 0; list-style: none; display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.5.0/ol.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.5.0/ol.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.15/angular-ui-router.min.js"></script> <div ng-app="app"> <ul> <li><a ui-sref="rome">Rome</a> </li> <li><a ui-sref="bern">Bern</a> </li> </ul> <div ui-view></div> </div> 

UPDATE UPDATE

To get map instance from directive, callback can be used from directive with map instance value once map is initialised. 要从指令获取map实例,可以在初始化地图后从带有指令的实例实例使用回调。 working code can be seen here JsFiddle . 可以在这里看到工作代码JsFiddle

检查此插件http://angular-ui.github.io/angular-google-maps/#!/ ,我正在使用它,它具有用于创建地图的完整工具

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

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