gpt4 book ai didi

javascript - $stateParams 返回空对象

转载 作者:行者123 更新时间:2023-12-03 05:57:01 31 4
gpt4 key购买 nike

我正在尝试从 URL 获取纬度和经度变量,并将它们分配给 Google map 的“center”属性。我没有收到任何错误,但我提醒屏幕中心对象,“纬度”和“经度”都为空。

这是网页:http://alainwebdesign.ca/pl4#/49.2/-122.6

Javascript:

(function (window, ng) {
ng.module('app', ['uiGmapgoogle-maps', 'ui.router'])



.config(function ($stateProvider) {
$stateProvider.state('location', {
url: '/:lat/:lon',
templateUrl: 'searchRadius.html',
controller: 'MapsCtrl',

resolve: {
resolveMap: function (MapService, $stateParams) {
return MapService.getData($stateParams.lat, $stateParams.lon);
}
}
});

})

.config(['uiGmapGoogleMapApiProvider', function (GoogleMapApi) {
GoogleMapApi.configure({
key: 'AIzaSyCbRPhVlxgVwBC0bBOgyB-Dn_K8ONrxb_g',
v: '3',
libraries: 'weather,geometry,visualization'
});
} ])


.controller('MapsCtrl', ['$scope', "uiGmapLogger", "uiGmapGoogleMapApi", "$interval", "$state", "$stateParams",
function ($scope, $log, GoogleMapApi, $interval, $state, $stateParams) {
$log.currentLevel = $log.LEVELS.debug;
var center = { latitude: parseFloat($stateParams.lat), longitude: parseFloat($stateParams.lon) };
alert(JSON.stringify(center));
//Object.freeze(center); caused TypeError: Cannot assign to read only property ('latitude') ...

console.log($stateParams);

$scope.map = {
center: center,
pan: false,
zoom: 16,
refresh: false,
events: {},
bounds: {}
};

$scope.map.circle = {
id: 1,
center: center,
radius: 500, //(current time - date lost)*km/hour
stroke: {
color: '#08B21F',
weight: 2,
opacity: 1
},

fill: {
color: '#08B21F',
opacity: 0.5
},
geodesic: false, // optional: defaults to false
draggable: false, // optional: defaults to false
clickable: true, // optional: defaults to true
editable: false, // optional: defaults to false
visible: true, // optional: defaults to true
events: {
dblclick: function () {
$log.debug("circle dblclick");
},
radius_changed: function (gObject) {
var radius = gObject.getRadius();
$log.debug("circle radius radius_changed " + radius);
}
}
}





//Increase Radius:
$interval(function () {
$scope.map.circle.radius += 30; //dynamic var
$state.transitionTo('location', { //location is the state name
center: $stateParams.center,
radius: $scope.map.circle.radius
},
{
notify: false
});
}, 1000); //end of interval function


} ]); //end of controller

})(window, angular);

searchRadius.html:

<div style="height: 100%"> <!--took out: ng-if="map.center !== undefined"-->
<ui-gmap-google-map
center='map.center'
zoom='map.zoom'
draggable='map.draggable'
dragging='map.dragging'
refresh='map.refresh'
options='map.options'
events='map.events'
pan='map.pan'>


<ui-gmap-circle
center='map.circle.center'
radius='map.circle.radius'
fill='map.circle.fill'
stroke='map.circle.stroke'
clickable='map.circle.clickable'
draggable='map.circle.draggable'
editable='map.circle.editable'
visible='map.circle.visible'
events='map.circle.events'>

</ui-gmap-circle>


</ui-gmap-google-map>

</div>

index.html:

   <!DOCTYPE html>
<html ng-app="app">

<head>
<link rel="stylesheet" href="example/assets/stylesheets/example.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="website_libs/dev_deps.js"></script>
<script src="https://code.angularjs.org/1.3.6/angular.js"></script>
<script src="dist/angular-ui-router.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.underscore.js"></script>
<script src="http://cdn.rawgit.com/nmccready/angular-simple-logger/0.0.1/dist/index.js"></script><script src="dist/angular-google-maps_dev_mapped.js"></script>
<script src="getLoc.js"></script>
<script src="searchRadius.js"></script>

<script src='//maps.googleapis.com/maps/api/js?key=AIzaSyD_g7xCYEi-U54SYfTXQ_lukRsChkWgjXQ'></script>

<title>Pet Locate</title>

<!--NEW STUFF FROM TOM-->


</head>

<body style="height: 100%">
<ui-view></ui-view>
</body>

</html>

最佳答案

您的问题是您没有使用 <ui-view>并使用 ng-controller 在页面中提供该路线的模板.

然后使用index.html作为路由的模板。

实际上,您有 2 个 Controller 实例,并且应用程序在路由方面根本没有正确设置

在index.html中运行的主ng-controller实例将运行,无论路由如何,这就是为什么它不等待$stateParams或路由更改

根据路由器文档使用其自己的模板设置页面,并且只有 <ui-view>在index.html中

关于javascript - $stateParams 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39883370/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com