AngularJS: separete create / edit page in control page

I have a single page app and three pages: ManageList, CreateList, EditList I use one controller for it and two views (manageView and CreateEditView)

when('/manageList', {
    templateUrl: settings.baseurl + '/app/modules/sample/list-partial.html',
    controller: 'listController',
    resolve: {
        listName: '$route',


        function ($route) {
            return {
                editList: false,
                manageList: true;

            }
        }]
}
}).
when('/createManageList', {
    templateUrl: settings.baseurl + '/app/modules/sample/createEdit-partial.html',
    controller: 'listController'
},
resolve: {
    listName: '$route',


    function ($route) {
        return {
            editList: false,
            manageList: false;

        }
    }]
}
}).
when('/editList/listName/:listName', {
    templateUrl: settings.baseurl + '/app/modules/sample/createEdit-partial.html',
    controller: 'listController',
    resolve: {
        listName: '$route',


        function ($route) {
            return {
                editList: $route.current.params.listName,
                manageList: false;

            }
        }]
}
}).

      

In listController I manage the code that will be executed with the listName and manageList parameters

if (listName.editList) {
    //code for editing goes here
} else if (listName.manageList) {
    //code for managing goes here

} else {
    // code for creating goes here 
}

      

The question is: is there a better way to split the logic to create the page. I do not like this evil, if otherwise.

thank

+3


source to share


1 answer


You have to create 3 controllers (one for each view). Once you have common behavior, you can create a service or factory, declare inside functions, and inject them into all the required controllers.



The controller needs to be tiny, you don't have to declare all your code logic inside them.

+1


source







All Articles