Reputation: 129
I have two controllers:
<div ng-controller="Main">
<div ng-controller="Map"></div>
</div>
In controller Main
I have variable $scope.mapCoord;
How I can to pass this variable in controller Map
?
Upvotes: 0
Views: 260
Reputation: 1464
In your "Map" controller, set "Main" scope to current scope such as below :
app.controller('Map', ['$scope','$controller',function($scope, $controller) {
$controller('Main', {
$scope : $scope
});
}]);
After that you can access all the scope of Main controller from his son controller :
app.controller('Map', ['$scope','$controller',function($scope, $controller) {
$controller('Main', {
$scope : $scope
});
var coord = $scope.mapCoord;
}]);
Upvotes: 0
Reputation: 10687
Use a service. For example:
var app = angular.module('myApp', [])
app.service('sharedProperties', function () {
var mapCoord= 'Test';
return {
getProperty: function () {
return mapCoord;
},
setProperty: function(value) {
mapCoord= value;
}
};
});
Inside your Main controller
app.controller('Main', function($scope, sharedProperties) {
$scope.mapCoord= sharedProperties.setProperty("Main");
});
Inside your Map controller
app.controller('Map', function($scope, sharedProperties) {
$scope.mapCoord= sharedProperties.getProperty();
});
Here's a fiddle for you. JSFiddle
Upvotes: 2
Reputation: 22001
You use events to pass it between controllers using by $broadcast
, $emit
and $on
Working with $scope.$emit and $scope.$on
Upvotes: 0