mgibas
mgibas

Reputation: 408

Controllers granularity in AngularJs

Maybe it's silly question but I'm coming from Asp.net MVC world and normal manner is t create single controller per model ie.:

public class FooController : Controller
{
    public ActionResult Index()
    {...}
    public ActionResult Details(long id)
    {...}
    public ActionResult Edit(long id)
    {...}
}

On the other hand in AngularJs app I'm creating controller per view like:

app.controller('FooDetailsController', function ($scope, $routeParams, FooService) {
    $scope.foo = FooService.get({ id: $routeParams.id })
});

app.controller('FooListController', function ($scope, FooService) {
    $scope.foos = FooService.query();
});

...

How should I create controllers in AngularJs, what is best practice? This one looks really awkward for me.

Upvotes: 0

Views: 193

Answers (1)

Sharondio
Sharondio

Reputation: 2614

Basically you're checking the $routeParam to see if there is a detail to look for, if not, looking for the list. http://plnkr.co/edit/jAK31F?p=preview

Upvotes: 1

Related Questions