vendredi 30 novembre 2018

AngularJS: Is it a good practice to have multiple functions in controller?

I have a controller which has four functions bind to scope. These functions will be called when user clicks on buttons(using ng-click).

Example:

.controller('myController', function($scope){
    $scope.myVariable = 0;
    $scope.funcOne = function(){
        // modify $scope.myVariable
        ...
    };
    $scope.funcTwo = function(){
        ...
    }
    // more functions...
});

Inside these functions, they contain some logic like modifying scope variables based on user input, so that the scope variable's change can reflect on DOM. (For example: in above code, myVariable is modified in funcOne, this will reflect on DOM.)

Some online resources indicates that main logic should be contained in service, then inject the service into the controller.

My questions:

  • is this a good practice?
  • Should controller contain a lot of logic?
  • If the logic should be contained in service, what should be contained in controller? and how would you do the dependency injection?

Thanks!

Aucun commentaire:

Enregistrer un commentaire