1

I have been new to angular js. My structure of Angular JS in MVC5 is in the picture belowApplication structure

I need to create a simple application that has CRUD for a single entity. So far, I have created only single controller in Angular folder.

I have put all the directives, filters and functions in the Website.js file. I need to create different pages like add/edit/details.

The CRUD operations are not only simple, but i have to put some extra logics in them. I need some process where either deletion, addition or updating in model do changes everywhere on the page.

Question

Should i place all the directives, functions to the same controller? Or i should create seperate files for each CRUD operation? Please guide me to the structure of this application.

1 Answer 1

1

You should create seperate files for directives, filters, services and controllers like this:

app(folder inside scripts)
----- controllers/(folder inside app)
---------- userController.js
---------- itemController.js
----- directives/(folder inside app)
---------- mainDirective.js
---------- otherDirective.js
----- services/(folder inside app where all the services will go)
---------- userService.js
---------- itemService.js
----- app.js(main file where you will declare angular module and other application configurations which are common)

Create module wise controllers and service like for user module i have created one service named userService and one controller userController now in this user controller i will implement all business logic related to user and all CRUD operations. It is good practice to specify all $http requests for CRUD in a service and call that service from controller.

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

6 Comments

Where would i register all the controllers, directives, services? would i be adding there dependencies in app.js file?
since I have only one CRUD thing i added as Angular (root folder) -- controllers -- directives -- services -- app.js
you will register all controllers, directives, services etc in your app like this: var app=angular.module('myApp',[]); Now all other things will be created like app.controller......app.service....app.directive
and when i need to use service in controller. i would pass that to controller. Why is that so? If i have created service for app as app.service('xyz',[]). why should add its dependency to controller if i have already added it to app?
In the app we have declared our service. All controllers, services, filters and directives are declared in app. we can not use a service directly in a view.. so we should create a controller for every view and then we can pass service as a dependency in that controller. Service are also used for reusability propose so we can pass one service in multiple controllers also.
|

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.