javascript - is it possible using multiple controllers on one controller in angularjs? -
so let have multiple controller
angular.module('myapp.controllers', []). controller('myctrl1', ['$scope', function($scope) { //define $scope.controller1 here $scope.controller1 = "controller1"; }). controller('myctrl1', ['$scope', function($scope) { //define $scope.controller2 here $scope.controller2 = "controller2"; }). controller('myctrl3', ['$scope', function($scope) { //i want property of $scope.controller1 , //$scope.controller2 here });
as can see above, example want access $scope property controller1 , controller2 3...
the question is, angularjs capable of calling multiple controller on 1 controller ?
in angularjs, scopes inherited based on position in dom. if html looked like:
<div ng-controller="my-ctrl1"> <div ng-controller="my-ctrl2"> <div ng-controller="my-ctrl3"></div> </div> </div>
then $scope.controller1 accessible myctrl1, myctrl2, , myctrl3, $scope.controller2 available myctrl2 , myctrl3.
if want share data between controllers, should store data in service , inject service controllers:
angular.module('myapp.controllers', []). factory('mydata', function(){ var mydata= {}; return mydata; }). controller('myctrl1', ['$scope' 'mydata', function($scope, mydata) { mydata.controller1 = "controller1"; }). controller('myctrl2', ['$scope', 'mydata', function($scope, mydata) { mydata.controller2 = "controller2"; }). controller('myctrl3', ['$scope', 'mydata', function($scope, mydata) { // can access mydata.controller1 , mydata.controller2 });
Comments
Post a Comment