0

Let's say we are implementing CRUD operations for a specific object - those view would be very similiar and i think i should use the same view with multiple controllers. Is there a way to choose the controller dinamicaly based on a parameter or this type of action can be only taken inside the controller?

2 Answers 2

1

You technically can, but according to the exellent angular styleguide from Johnpapa, style Y037 :

Define a controller for a view, and try not to reuse the controller for other views.

Though, you're actually right thinking that some CRUD logic should be made common and abstracted. Controllers are just not the right place; Factories (ie services) are.

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

1 Comment

Sorry I misread. I'm having trouble figuring out what you mean view would be very similar. Can you provide an example ?
0

You can use the same view on different controllers but it depends on what you are doing inside the view and whether or not the controller has the necessary members within the $scope object that are bound in the view. You can add an "action" variable on your $scope object and modify the view based on the same.

2 Comments

You can also achieve this via routing - let the routing parameter dictate the 'action' and have the same view with multiple controllers.
Routing is a better option.

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.