2013-05-16 84 views
0

當我使用jQuery的$.ajax它的工作原理每次,但是當我回來Angularjs我寫

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> 

而且它永遠不會奏效。甚至在Chrome的調試程序:-(

的請求可能的專家可以在幾秒鐘內我做錯了什麼看?

+0

的Merci beaucoup爲了您的編輯,我在這段時間學到了很多東西,還有如何提問Stackoverflow – LeGilles

回答

2

我認爲這個問題是你有沒有控制器。

<!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. 
     }); 
    }; 
    };  
+0

非常感謝,現在它工作的很好! – LeGilles

+0

很高興幫助。 –

相關問題