Rishi Prakash
Rishi Prakash

Reputation: 1779

How to add functions defined in one scope into another, without duplicating?

I have 2 controllers 1)controller1 2)controller2

HTML code is something like

<div>
<div>
All time Visible
<input ng-change=callAMethodInController1()>
</div>
<div ng-show=toShowTab1Div id="tab_2_div">tab1</div>
<div id="tab_2_div" ng-show=toShowTab2Div class="PR1 PL1  PT2">
     <div ng-include="'../style/taxation.html'"></div>
</div>
</div>

This whole html file is taxPare.html.

RouteProvider is like

$routeProvider
    .when('/', {
        templateUrl: '../jspfolder/payroll/taxPare.html',
        controller: 'controller1 '
    })
    .when('/taxCalc', {
        templateUrl: '../jspfolder/payroll/taxPare.html',
        controller: 'controller2'
    })

Till this I am completely able to inter-mingle code of both controllers. But there are some problems.

1)I have a portion of code which is inclusive to both the controllers. 2)Some methods are exclusively written in controller1.

But when I am on controller2 ,and I make some changes in common part, as obvious nothing happens because $scope.methodOfController1 are not accessible,

Solution of this??

Other than this is is possible to add one js file into inmiddle of another.It's like copying one js inside another, but without duplicating the code?

Upvotes: 0

Views: 49

Answers (1)

svnm
svnm

Reputation: 24308

You could put the common code into a Service and inject this into each controller

module.service('userService', function(){
  this.users = ['John', 'James', 'Jake'];
});

or a factory method

module.factory('userService', function(){
  var users = {['John', 'James', 'Jake']};
  return users;
});

Upvotes: 1

Related Questions