Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Using one controller for many coherent views across multiple HTTP requests

I have a simple Angular JS scenario. I show a list of experts in one view, and that list contains actions to edit an expert, which would be done in another view. These views are coming from server and are not in one file. They don't load together.

<!-- experts.html -->
<div ng-controller='expertController'>
    <!-- showing the list of experts here -->
</div>

And in another file on the server I have:

<!-- expert.html -->
<div ng-controller='expertController'>
    <!-- a form to edit/add an expert -->
</div>

And my controller is:

app.controller('expertController', function ($scope) {
    $scope.getExperts = function () {
        _.get('/expert/getexperts/', null, function (data) {
            $scope.$apply(function () {
                $scope.experts = data;
            });
        });
    };

    $scope.editExpert = function (index) {
        _.get('/expert/getexpert/', { id: $scope.experts[index].Id }, function (data) {
                $scope.$apply(function () {
                    $scope.expert = data;
                });
         });
    };
});

However, no data would be shown in the edit form. I inspected scopes using Batarang, but expert object won't be shown.

The problem seems to be that I use one controller for two (and optionally more than two) views. However, as I've read some other questions on SO, it seems that the best practice is to have one controller per HTML view.

However, I think it's more coherent and consistent to have one expertController to do all CRUD operations, alongside other actions related to expert. I think it's a little ugly to have expertEditController for edit form and expertListController for the list HTML.

What is the best practice here? How can I have a coherent and semantically named controller, and at the same time support many views. In any MVC framework that I've seen till now, it's something routine to support many views via one controller.

like image 984
Hamid Barani Avatar asked Dec 01 '13 12:12

Hamid Barani


People also ask

How to handle multiple HTTP requests in angular?

There are multiple ways to handle multiple requests; they can be sequential or in parallel. In this post, we will cover both. Let's start with a simple HTTP request using the Angular Http service. In our app, we have just a single component that pulls in Angular's Http service via Dependency Injection.

What is httpclient API in angular 12?

This step by step guide helps you ascertain the usage, implementation, on top of that, the benefits of HttpClient API in the Angular 12 application. It also enables you to answer how to make HTTP (HTTP POST, GET, PUT, and DELETE) Requests. Angular is a powerful and profound framework to makes the frontend job easy for frontend developers.

How angular 11’s httpclient service and RxJS helps in building spas?

More often than not when building SPAs, we need to aggregate data from multiple REST API endpoints and render the fetched data on the UI. Making asynchronous requests and working with them can be intimidating but thanks to the Angular 11’s HttpClient service and the RxJS library, it can be implemented more easily.

How to enable navigation in angular?

To enable the navigation add the router-outlet directive in app.component.html file: We will explain to you how to import and inject HttpClientModule in the Angular application. Import the HttpClientModule from the ‘@angular/common/http’ library. Also, import and register and FormsModule in the same file:


1 Answers

Of course you can use many views with the same controller. For example: for editing and adding new items.

But you have to remember that each time the view is loaded the controller is initialized and fresh, new $scope is injected.

In your case it would be better to just use two separate controllers (one for experts and one for expert) and use -- optionally -- some service to communicate between those and maybe provide some caching mechanism.

UPDATE In case you want to provide some common/shared functionality between controllers the best way to go is to create and then inject a separate service. It keeps the code DRY.

like image 136
artur grzesiak Avatar answered Sep 17 '22 20:09

artur grzesiak