Would it be interesting directives using http.get?

0

This is a html piece:

<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 as follows, if it would be interesting to put this html as a directive and use the directive itself to perform the same controller function, to fill the projects array and display the html. >     

asked by anonymous 22.04.2017 / 04:33

1 answer

2

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

First, the ideal would be to use component instead of directive to follow the new Angle patterns (2, 3, 4 ...). Then you must execute the $http calls and then store the information in service . Then, in your% of_%, you just ask the data for the service and it displays.

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

    
22.04.2017 / 05:07