简体   繁体   English

如何将数据从controller.js传递到map.js?

[英]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.

相关问题 无法打印由controller.js获取的数据 - Unable to print the data fetched by the controller.js 如何使用JavaScript Express节点将controller.js中一个方法中的变量公开给另一个controller.js中的变量 - How to expose a variable from one method within a controller.js to another controller.js with javascript express node AngularJS 1-如何将app.js值列表传递给controller.js - AngularJS 1 - How to pass app.js list of values to controller.js 如何在controller.js中使用过滤器比较日期 - How to compare date using filter in controller.js 如何在“ angular.module”文件中“导入” controller.js文件 - How to “import” a controller.js file in the angular.module file 如何从模式弹出窗口中调用controller.js内部定义的函数 - How do I call a function defined inside controller.js from a modal popup 如何将Google Firebase链接到Ionic的controller.js文件? - How to link google firebase to ionic's controller.js file? 如何将controller.js包含在ng-include html页面中? - how to include controller.js into ng-include html page? 如何从angularjs中的controller.js文件到html文件获取值 - How to get a value from controller.js file to html file in angularjs 带有Controller.js括号的AngularJS Datepicker - Datepicker for AngularJS with brackets on Controller.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM