0

When I use jQuery's $.ajax it works everytime, but when I come back to Angularjs I write

test.js :

var readContracts = function($scope, $http){
    $http({method: 'GET', url: 'http://test.url.com/contracts'}).
    success(function(data, status, headers, config) {
     $scope.contracts = angular.fromJson(data);

    }).
    error(function(data, status, headers, config) {
    debugger;
    // called asynchronously if an error occurs
    // or server returns response with an error status.
    });
};    

test.html :

<!DOCTYPE html>
<html ng-app>
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body>
        <button ng-click="readContracts()">readContracts</button>
        <ul>
            <li ng-repeat="contract in contracts"> {{contract | json}} </li>
        </ul>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
        <script src="test.js"></script>
    </body>
</html>

And it never works. Not even the request in the chrome's debugger :-(

May an expert could see in few seconds what I did wrong ?

1
  • Merci beaucoup for your edit, I learn a lot in this time, also how to ask questions on Stackoverflow Commented May 16, 2013 at 22:29

1 Answer 1

1

I think the problem is you have no controller.

<!DOCTYPE html>
<html ng-app>
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body ng-controller="ContractsController">
        <button ng-click="readContracts()">readContracts</button>
        <ul>
            <li ng-repeat="contract in contracts"> {{contract | json}} </li>
        </ul>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">     </script>
        <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js">    </script>
        <script src="test.js"></script>
    </body>
</html>


 function ContractsController($scope, $http){
    $scope.readContracts = function(){
        $http({method: 'GET', url: 'http://test.url.com/contracts'}).
        success(function(data, status, headers, config) {
             $scope.contracts = angular.fromJson(data);

        }).
        error(function(data, status, headers, config) {
        debugger;
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        });
    };
   };    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.