user3452075
user3452075

Reputation: 461

AngularJS - Similar view - Should i use same view with multiple controllers?

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?

Upvotes: 0

Views: 223

Answers (2)

Cyril CHAPON
Cyril CHAPON

Reputation: 3747

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.

Upvotes: 1

Ketan
Ketan

Reputation: 5891

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.

Upvotes: 0

Related Questions