There is a typo on the first controller- ng-contoller="controller1"
. If that gets corrected, then the controller will be created correctly, and controller2 will inherit the scope from controller1 (refer to the Angular docs for Scope inheritance) and see the example below.
Also note that in the HTML we don't need to reference the controller variable - omit that object, since we can access properties on the scope directly. So instead of:
<div ng-controller="controller2" ng-init="controller2.someMethod(controller1.Data.someId)">
Do this:
<div ng-controller="controller2" ng-init="someMethod(Data.someId)">
var app = angular.module('app', []);
app.controller('controller1', ['$scope',
function($scope) {
$scope.Data = {
someId: 3
};
$scope.something = 'something text';
}
]);
app.controller('controller2', ['$scope',
function($scope) {
$scope.someMethod = function(id) {
console.log('id: ', id, ' $scope.Data: ', $scope.Data);
};
}
]);
<script src="https://code.angularjs.org/snapshot/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="controller1">
<div ng-controller="controller2" ng-init="someMethod(Data.someId)">
{{something}}
</div>
</div>
</div>
Update:
Since you updated your question to have more detail (e.g. Controllers specified with an alias), I have taken that angular HTML and alterred it to the code below. Notice how you don't really need to reference the aliases within the inline calls?
And note that I did simplify the button so we don't need an extra directive for that.
var app = angular.module('app', []);
app.controller('routesController', ['$scope',
function($scope) {
console.log('routesController created');
$scope.RoutesData = {
apiRouteInfoResponse: "43 miles"
};
$scope.something = 'something text';
$scope.addTickAndRouteRating = function() {
console.log('addTickAndRouteRating() called');
};
$scope.loadFromRouteInfoScreen = function(response) {
console.log('loadFromRouteInfoScreen() called - response:',response);
};
}
]);
app.controller('routeController', ['$scope',
function($scope) {
$scope.someMethod = function(id) {
console.log('id: ', id, ' $scope.Data: ', $scope.Data);
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="routesController as routesCtrl">
<div class="ps-top-space-1" ng-controller="routeController as routeCtrl" ng-init="loadFromRouteInfoScreen(RoutesData.apiRouteInfoResponse)">
<button modifier="large" ng-click="addTickAndRouteRating()">Rate route or Add tick...</button>
</div>
</div>
</div>