Angular index variable

Asked

Viewed 382 times

0

Good morning guys, could anyone help me, I need to take the value of the index variable from the angular to a small php block, to do the removal of it in the database.

The way it is I can’t, gives an error in the $Cope method.

ERROR:

 <script type="text/javascript">
            var myApp = angular.module('app', []);            
            myApp.controller('alunosCad', ['$scope', function ($scope) {
                    $scope.alunos = [                        
                        <?php                        
                        $alunosDataBase = DaoAluno::selectAll();                                     
                        $count = count($alunosDataBase);             
                        $i = 0;
                        foreach ($alunosDataBase as $alunosCad) {                            
                            if($i === ($count - 1)){
                                echo '{codigo: "'.$alunosCad->getId_aluno().'", nome: "'.$alunosCad->getNm_aluno().'", email: "'.$alunosCad->getMail_aluno().'"}';
                            }else{
                                echo '{codigo: "'.$alunosCad->getId_aluno().'", nome: "'.$alunosCad->getNm_aluno().'", email: "'.$alunosCad->getMail_aluno().'"},';
                            } 
                            $i+=1;
                        }           
                        ?>
                    ];                    
                    $scope.remover = function(index){
			$scope.alunos.splice(index, 1);                        
                        <?php                        
                        $index = "<script>document.write(index)</script>"; 
                        $aRemover = $alunosDataBase[$index];                        
                        DaoAluno::delete($aRemover->getId_aluno());
			?>                        
                    };                
                }]);
        </script> 

  • 1

    Dude you’re doing it wrong, you don’t use php code in the middle of Angular. You will have to take the database data and return in JSON format, usually this request is made by REST.

  • Ulysses, do not post pictures of your code. Copy and paste your own code here as text. And use Stackoverflow’s own tool to format the code.

  • All right. Thank you..

1 answer

0


Dude you’re doing it wrong, you don’t use php code in the middle of Angular. You will have to take the database data and return in JSON format, usually this request is made by REST.

Follow an example:

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .then(function (response) {$scope.names = response.data.records;});
});
<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr ng-repeat="x in names">
    <td>{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>
</body>
</html>

Example link w3schools

  • Sorry is my first contact with angular.. as the file will return something... I didn’t quite understand how to use..

  • Do you read this link http://imasters.com.br/development/exemplo-de-integracao-rest-entre-servidor-e-cliente/? trace=1519021197&source=single

  • You are using which php framework?

  • I used bootstrap, on some page I’m using jquery, but I still don’t know very well use, I have only one month dealing with php...

  • How could I reference this site if it is not yet hosted for example the file .. /_php/listalunos.php

  • just put "_php/listalunos.php", it doesn’t need to be the address of your site but the file.

  • It would be this way.. <script type="text/javascript"> var app = angular.module('app', []); app.controller('alunosCad', Function ($Scope, $http) { $http.get("./_php/listalunos.php") .then(Function (Response) { $Scope.students = Response.data.Records; }); $Scope.remove = Function (index) { $Scope.alunos.splice(index, 1); }; }); </script>

  • If I wanted to remove some with check box

  • I did not understand the way I could do to remove directly on the bank and on the table with angular.

  • Then to remove several you need to make a request by POST with the angular, ai your php page will take the data(in case the ids) and remove them

Show 5 more comments

Browser other questions tagged

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