Creating a generic angularjs listController

馋奶兔 提交于 2019-12-01 19:43:09
XelharK

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!