G.Mounika
G.Mounika

Reputation: 425

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.

This is my 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.

Upvotes: 0

Views: 85

Answers (3)

mkaran
mkaran

Reputation: 2718

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); 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). E.g. $scope.map = new Map(); $scope.map.range = .... etc. Hope this helps! (localstorage as mentioned could be a good solution also, but it all depends on what you want to do)

Upvotes: 0

Qianyue
Qianyue

Reputation: 1777

If your map.js has no related to Angular, you can use localStorage to keep your range.

So in your controller, inject $localStorage, and set the variable in the 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');. Here you can find de doc of MDN for Storage.

Upvotes: 1

Ranjith Jayaram
Ranjith Jayaram

Reputation: 299

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. Else you could use localstorage or session storage

Upvotes: 0

Related Questions