2017-07-18 23 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事件,但它工作正常,但我遇到了這個問題 – JJJ

+1

顯示您的文件夾結構以及您使用的角度版本是什麼? – Vivz

回答

1

腳本標記在您的案例中是錯誤的。您正在使用小寫字母在你的代碼,但你的文件夾結構顯示大寫腳本

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

更新 如果你使用的是最新版本的angularjs,請嘗試下面的代碼,因爲成功和錯誤已被棄用。

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

它顯示的是html文件字符串 – JJJ

+0

'success'和'error'是完全有效的,除非Angular版本是由OP指定的。 – 31piy

+0

我已更新代碼以顯示html ...使用** $ sce.trustAsHtml **和** ng-bind-html ** –

相關問題