TiggerToo
TiggerToo

Reputation: 670

Repeated scope defining method in AngularJS

Building a site with a bunch of different card games. Each game has its own controller, but there are some functions that are duplicated in all of the games. Is it possible to extract the following code from all of these games into one game. It seems like the inheritance in JavaScript is wonky enough that that might not be useful? I don't know.

setScope = function(obj) {
  $scope.game = obj.game;
  $scope.activePlayer = obj.active_player;
  $scope.players = obj.players;
}

Upvotes: 1

Views: 33

Answers (1)

Olivierodo
Olivierodo

Reputation: 406

in angular you could inherit on this way (it sounds like trait) :

parent

app.controller('gameCtrl',[function(){
    $scope.init = function(obj) {
        $scope.game = obj.game;
        $scope.activePlayer = obj.active_player;
        $scope.players = obj.players;
    };
}]);

* child *

app.controller('game1Ctrl', [$controller, function($controller){
    $controller('gameCtrl',{$scope:$scope});

    var obj  = {};
    $scope.init(obj);
});

Upvotes: 1

Related Questions