Creating a generic angularjs listController

后端 未结 1 1041
眼角桃花
眼角桃花 2021-01-19 18:08

Angular ui-router allows me to resolve different injections that I can use in my controllers.

I created a simple generic ListController, and in the ui-router I am in

相关标签:
1条回答
  • 2021-01-19 18:29

    For future reference, I ended up writing a system of generic controllers, and tiny specialized controllers that extend them.

    For the example above, the generic controller is subclassed by both "PeopleController" and "CarController". At the beginning of the controller code, each one of them will do something like:

    controller('PeopleController', ['$scope', 'peopleService', function ($scope, peopleService) {
    angular.extend(this, $controller('listController', {$scope: $scope, functions:{
    
            add: peopleService.addPerson,
            remove: peopleService.removePerson,
            [...]
    
        }}));
    
        $scope.people = $scope.itemList;
    
    }
    

    So now I can instantiate the controllers on their own even in the ng-controller directive, and they will use the generic controller for all of the CRUD methods.

    0 讨论(0)
提交回复
热议问题