[英]How to pass data from controller.js to map.js?
I want to use the value of $scope.data.range
from controller.js in map.js. 我想使用
$scope.data.range
中controller.js中$scope.data.range
的值。
This is my controller.js 这是我的controller.js
angular.module('starter.controllers', [])
.controller("LocController",function($scope,$rootScope){
$scope.data.range=5000;
})
How can I use this $scope.data.range
in map.js which is a simple javascript file. 如何在map.js(这是一个简单的javascript文件)中使用此
$scope.data.range
。
If your map.js has no related to Angular, you can use localStorage to keep your range. 如果您的map.js与Angular没有关系,则可以使用localStorage保持范围。
So in your controller, inject $localStorage
, and set the variable in the localStrorage: 因此,在您的控制器中,注入
$localStorage
,并在localStrorage中设置变量:
.controller("LocController", function($scope, $rootScope, $localStorage){
$scope.data.range = 5000;
$localStorage.range = $scope.data.range;
})
And then in your map.js, get the localStorage by localStorage.getItem('range');
然后在您的map.js中,通过
localStorage.getItem('range');
获取localStorage localStorage.getItem('range');
. 。 Here you can find de doc of MDN for Storage.
在这里,您可以找到MDN for Storage的文档。
I guess map.js comes under the same angular.module('starter.controllers', []), and if you dont want to go for sessions , you could use a service or factory. 我想map.js属于同一angular.module('starter.controllers',[]),如果您不想参加会话,则可以使用服务或工厂。 Else you could use localstorage or session storage
另外,您可以使用本地存储或会话存储
I am not sure I can give a full solution since don't know how map.js is structured but you could, for example, add a function within the map.js like setDataRange(dataRange);
我不确定我能给出一个完整的解决方案,因为不知道map.js的结构如何,但是您可以在map.js中添加一个函数,例如
setDataRange(dataRange);
and access this from the controller. 并从控制器访问它。 Or in general access and update anything you want from the controller (provided you have included the map.js script before the controller.js).
或者,一般而言,访问并更新您想要从控制器获取的任何内容(前提是您已在controller.js之前包含map.js脚本)。 Eg
$scope.map = new Map(); $scope.map.range = ....
例如
$scope.map = new Map(); $scope.map.range = ....
$scope.map = new Map(); $scope.map.range = ....
etc. Hope this helps! $scope.map = new Map(); $scope.map.range = ....
等等。希望这对您$scope.map = new Map(); $scope.map.range = ....
帮助! (localstorage as mentioned could be a good solution also, but it all depends on what you want to do) (如上所述,本地存储也可能是一个很好的解决方案,但这取决于您要执行的操作)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.