为什么无法从$http服务获取数据? 第一个AngularJS应用程序 响应数据:{{Data}} 错误:{{Error}} var myApp=angular.module('myAngularApp',[]); controller(“myController”,函数($scope,$http){ var onSuccess=函数(数据、状态、标题、配置){ $scope.data=数据; }; var onError=函数(数据、状态、标题、配置){ $scope.error=状态; } var promise=$http.get(“index.html”); 承诺。成功(成功); 承诺.错误(onError); });

为什么无法从$http服务获取数据? 第一个AngularJS应用程序 响应数据:{{Data}} 错误:{{Error}} var myApp=angular.module('myAngularApp',[]); controller(“myController”,函数($scope,$http){ var onSuccess=函数(数据、状态、标题、配置){ $scope.data=数据; }; var onError=函数(数据、状态、标题、配置){ $scope.error=状态; } var promise=$http.get(“index.html”); 承诺。成功(成功); 承诺.错误(onError); });,angularjs,Angularjs,使用然后使用而不是成功,使用捕获而不是错误 例如: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>First AngularJS Application</title> <script src="scripts/angular.js"></script> </head> <bo


使用然后使用而不是成功,使用捕获而不是错误

例如:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>First AngularJS Application</title>
    <script src="scripts/angular.js"></script>

</head>
<body ng-app = "myAngularApp">
<div>
        <div ng-controller="myController">
            Response Data: {{data}} <br />
            Error: {{error}}
        </div>
    </div>
    <script>
        var myApp = angular.module('myAngularApp', []);

        myApp.controller("myController", function ($scope, $http) {

            var onSuccess = function (data, status, headers, config) {
                $scope.data = data;
            };

            var onError = function (data, status, headers, config) {
                $scope.error = status;
            }

            var promise = $http.get("index.html");

            promise.success(onSuccess);
            promise.error(onError);

        });
    </script>
</body>

响应数据:
错误:{{Error}} var myApp=angular.module('myAngularApp',[]); controller(“myController”,函数($scope、$http、$sce){ var onSuccess=函数(数据、状态、标题、配置){ $scope.data=$sce.trustAsHtml(data.data); }; var onError=函数(数据、状态、标题、配置){ $scope.error=数据; } var promise=$http.get(“index.html”); 承诺。然后(成功); 承诺。抓住(一个错误); });
使用然后使用而不是成功,使用捕获而不是错误

例如:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>First AngularJS Application</title>
    <script src="scripts/angular.js"></script>

</head>
<body ng-app = "myAngularApp">
<div>
        <div ng-controller="myController">
            Response Data: {{data}} <br />
            Error: {{error}}
        </div>
    </div>
    <script>
        var myApp = angular.module('myAngularApp', []);

        myApp.controller("myController", function ($scope, $http) {

            var onSuccess = function (data, status, headers, config) {
                $scope.data = data;
            };

            var onError = function (data, status, headers, config) {
                $scope.error = status;
            }

            var promise = $http.get("index.html");

            promise.success(onSuccess);
            promise.error(onError);

        });
    </script>
</body>

响应数据:
错误:{{Error}} var myApp=angular.module('myAngularApp',[]); controller(“myController”,函数($scope、$http、$sce){ var onSuccess=函数(数据、状态、标题、配置){ $scope.data=$sce.trustAsHtml(data.data); }; var onError=函数(数据、状态、标题、配置){ $scope.error=数据; } var promise=$http.get(“index.html”); 承诺。然后(成功); 承诺。抓住(一个错误); });
脚本标记在您的情况下是错误的。代码中使用小写,但文件夹结构显示大写脚本

<div>
    <div ng-controller="myController">
        Response Data: <span ng-bind-html="data"></span> <br />
        Error: {{error}}
    </div>
</div>
<script>
    var myApp = angular.module('myAngularApp', []);

    myApp.controller("myController", function ($scope, $http, $sce) {

        var onSuccess = function (data, status, headers, config) {
            $scope.data = $sce.trustAsHtml(data.data);
        };

        var onError = function (data, status, headers, config) {
            $scope.error = data;
        }

        var promise = $http.get("index.html");

        promise.then(onSuccess);
        promise.catch(onError);

    });
</script>

有关详细信息

脚本标记在您的案例中是错误的。代码中使用小写,但文件夹结构显示大写脚本

<div>
    <div ng-controller="myController">
        Response Data: <span ng-bind-html="data"></span> <br />
        Error: {{error}}
    </div>
</div>
<script>
    var myApp = angular.module('myAngularApp', []);

    myApp.controller("myController", function ($scope, $http, $sce) {

        var onSuccess = function (data, status, headers, config) {
            $scope.data = $sce.trustAsHtml(data.data);
        };

        var onError = function (data, status, headers, config) {
            $scope.error = data;
        }

        var promise = $http.get("index.html");

        promise.then(onSuccess);
        promise.catch(onError);

    });
</script>

有关更多信息

您的angular文件在项目脚本/angular.js中的此位置上存在?是的。我已经尝试过angular js事件,它正在工作,但我在这一次遇到了问题显示你的文件夹结构和你使用的angular的版本?@Peterhdd它仍然是same@Vivz您可以单击文件夹结构查看您的angular文件是否存在于项目脚本/angular.js中的此位置???是的。我已经尝试过angular js事件,它正在工作,但我在这一次遇到了问题显示你的文件夹结构和你使用的angular的版本?@Peterhdd它仍然是same@Vivz您可以单击文件夹结构查看
success
,和
error
是完全有效的,除非OP指定了角度版本。我已经更新了代码以显示html。。。使用$sce.trustAsHtml和ng bind htmlyou可以看到$sce的任何数据值和错误值。页面中的输出只是:响应数据:错误:在视图端,您必须使用并在控制器中注入$sce,然后将响应数据标记为受信任的html$sce.trustAsHtml(Data.Data)<代码>成功和
错误
完全有效,除非OP指定角度版本。我已更新代码以显示html。。。使用$sce.trustAsHtml和ng bind htmlyou可以看到$sce的任何数据值和错误值。页面中的输出只是:响应数据:错误:在视图端,您必须使用并在控制器中注入$sce,然后将响应数据标记为受信任的html$sce.trustAsHtml(Data.Data);