2017-07-18 7 views
1
<!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> 

This is the html file and when I load the page the data were not retrieved. I'm not sure if I have some little mistakes since I copy pasted it in the tutorial. This will be the output.

Folder Structure

+0

あなたの角度のファイルは、プロジェクトのスクリプト/ angular.jsで、この場所に存在しています? –

+0

そうです。私はすでに角度jsイベントを試して、それが動作しているが、私はこの1つに問題があります – JJJ

+1

あなたのフォルダ構造と使用している角度のバージョンを表示しますか? – Vivz

答えて

1

あなたの場合、スクリプトタグが間違っています。成功とエラーが廃止されましたので、あなたは、あなたのコード内で小文字を使用しているが、あなたはangularjsの最新バージョンを使用している場合は、あなたのフォルダ構造が大文字スクリプト

<script src="Scripts/angular.js"></script> 

更新 を示し、以下のコードを試してみてください。より多くの情報については

var myApp = angular.module('myAngularApp', []); 

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

      var onSuccess = function (data) { 
       $scope.data = data.data; 
      }; 

      var onError = function (data) { 
       $scope.error = data; 
      } 

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

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

     }); 

Why are angular $http success/error methods deprecated? Removed from v1.6?

+1

ええ、それは今働いている感謝:) – JJJ

-1

使用その後、ではなく、成功キャッチではなく、エラーを使用

例:

<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> 
+0

これは、代わりに – JJJ

+0

'success'というhtmlファイルの文字列を表示し、' error'はAngularバージョンがOPによって指定されていない限り完全に有効です。 – 31piy

+0

htmlを表示するためのコードを更新しました。** $ sce.trustAsHtml **および** ng-bind-html ** –

関連する問題