Would it be interesting guidelines using http.get?

Asked

Viewed 49 times

0

This is a piece of html:

<tr ng-repeat="projeto in projetos">
    <td>{{projeto.name}}</td>
    <td>{{projeto.status}}</td>
  </tr>

It uses a controller that does the following function

$http.get('system/project/listProjectsPerUser')
.then(function (response){
    if(response.data.success){
        $scope.projetos = response.data.projects;
        console.log($scope.projetos);
    }
    else{
        console.log('Erro');
    }
});

My question is the following, if it would be interesting to put this html as a directive and use the directive itself to perform the same function of the controller, to fill the array projects and display the html.

1 answer

2


It will depend on your context, but generally not, because every time the directive is rendered, it will execute the call $http. Often the information does not change constantly so that we need to constantly redo the call. But if you need to upgrade in a short time interval, still this is the work of a service, not of u controller, directive or component.

First, the ideal would be to use component instead of directive to follow the new Angular patterns (2, 3, 4...). Then, you must execute the calls $http and subsequently store the information in a service. Then on his component you just request the data to the service and display.

In this way, your component can easily have HTML internally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.