1

I have a template like this.

<body ng-app="demo" ng-controller="demo">
  <div ng-include="/main.html">
  </div>
</body>

And the main.html is.

<div ng-app="main" ng-controller="main>
  ""
</div>

here is the js.

JS-1

var myapp = angular.module('demo', []);
myapp.controller('demo', function($scope,$routeParams, $route,$http) {
$scope.variable="444"
})

JS-2

var mainapp = angular.module('mainapp', []);
myapp.controller('main', function($scope,$routeParams, $route,$http) {

})

Is it possible to access the scope of JS-1 inside JS-2?, if yes how, if no is there any solution to this.Thanks.

vipin
  • 670
  • 2
  • 9
  • 25
  • If you don't mind, I would suggest to you to take some time and observe the ui-router: https://github.com/angular-ui/ui-router/wiki , there are som links explaining it more http://stackoverflow.com/a/24693129/1679310... from my pespective - this awsome state machine library for angular could do a lot for you... – Radim Köhler Jul 18 '14 at 09:34
  • Why dont you try service? – Francis Stalin Jul 18 '14 at 09:36

1 Answers1

1

It depend what you want to do. If you want read $scope.variable variable from JS-1, you should see it in JS-2 $scope. If you want modify $scope.variable form JS-1, you should create method in JS-1:

$scope.changes = function(data){
        $scope.variable = data;
}

This method also should be available in JS-2 $scope.

This isn't nice solution but should work. The best solution is to create service which will provide operations on JS-1 fields.

krzysiek.ste
  • 2,246
  • 1
  • 26
  • 24