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

测试.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 调试器中的请求 :-(

专家可以在几秒钟内看到我做错了什么吗?

4

1 回答 1

1

我认为问题是你没有控制器。

<!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.
        });
    };
   };    
于 2013-05-16T22:19:39.890 回答