0

As i tried to learn some Angular in the past weeks, i have adopted a certain application architecture.

Assuming a intermediate large SPA, i create the view and apply a maincontroller:

<html ng-app="app">
    <body ng-controller="appCtrl">
        <!-- All of the view goes in here -->
    </body>
</html>

Now i think about the stuff that my application will have to do. Assuming that this one would have to store some data and working with it, i would create a provider to build a "class".

    var data = angular.module('data', []);

data.provider('$dataObject',function(){

    this.$get = function($http){

            function DataObject(){

                var field = "testvalue";
                var object = {
                    // ...
                }
            }

            DataObject.prototype.processData = function(){
               // do something
            };

        return {
            Shipment: function(){
                return new Shipment();
            }
        }
    }
});

I would then create an instance of that class in my maincontroller, storing all of the necessary data from the view into that object, processing it by calling the class methods.

If the application would need another functionality, e.g a dialog to pick some stuff, i would repeat the steps above and create a class for that dialog.

Now, while i am quite comfortable to do things that way, i still wonder if someone would consider this bad practice, and if so, why is that and what could i do better?

1 Answer 1

1

Creating services/providers for your models/data (with APIs to expose the data), and then injecting them into controllers is the "Angular way."

When I design an angular app, I think about the models I need and create services for them. I then (or in parallel) think about the views and design those. I normally create custom directives at this point also. Lastly, each view gets a controller, whose job it is to glue the models/data from the services that the view needs. (Make controllers as thin as possible.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.