在角JS无法获得正确的数据 [英] Cannot obtain proper data in angular JS

查看:92
本文介绍了在角JS无法获得正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做在AngularJS以下。数据从提到的网址获得。
如果你打开​​网址,你可以看到它提供了回应。

不过,我不能够通过angularjs获得HTML里面的标题。
我得到一个空的结果。我究竟做错了什么?
有它是与JSON编码?

 <!DOCTYPE HTML>
< HTML NG-应用>
  < HEAD>
    &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js>&下; /脚本>
    <脚本>
    功能ActivitiesListCtrl($范围){
  $ http.get('http://www.omdbapi.com/?t=the%20shawshank%20redemption').success(功能(数据){
    $ scope.mydata =数据;
  }
}
 < / SCRIPT>
  < /头>
  <机身NG控制器=ActivitiesListCtrl>
  < H1>电影名称< / H1>
  < UL>
   <李NG重复=数据MYDATA>
     {{data.Title}}
   < /李>
  < / UL>
< /身体GT;
< / HTML>
< / HTML>


解决方案

有相当多问题与您的code :-(
语法错误,而不是注入$ HTTP的响应是一个单一的电影,而不是一个集合的。

请尝试以下方法。

 <!DOCTYPE HTML>
< HTML NG-应用>
  < HEAD>
    &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js>&下; /脚本>
    <脚本>
    功能ActivitiesListCtrl($范围,$ HTTP){
  $ http.get('http://www.omdbapi.com/?t=the%20shawshank%20redemption').success(功能(数据){
    $ scope.mydata =数据;
  });
}
 < / SCRIPT>
  < /头>
  <机身NG控制器=ActivitiesListCtrl>
  < H1>电影名称< / H1>
    {{mydata.Title}}
< /身体GT;
< / HTML>

与集合示例:

 <!DOCTYPE HTML>
< HTML NG-应用>
  < HEAD>
    &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js>&下; /脚本>
    <脚本>
    功能ActivitiesListCtrl($范围,$ HTTP){
  $ http.get('http://www.omdbapi.com/?s=True%20Grit').success(功能(数据){
    $ scope.mydata =数据;
  });
}
 < / SCRIPT>
  < /头>
  <机身NG控制器=ActivitiesListCtrl>
  < H1>电影名称< / H1>
    < UL>
       <李NG重复=DATA1在mydata.Search> {{data1.Title}}< /李>
    < / UL>
< /身体GT;
< / HTML>

I have done the following in AngularJS. The data is obtained from the URL mentioned. If you open the URL, you can see the response it provides.

However, I am not able to obtain the title inside HTML via angularjs. I am getting a blank result. What am I doing wrong? Has it something to do with JSON encoding?

<!doctype html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
    <script>
    function ActivitiesListCtrl($scope) {
  $http.get('http://www.omdbapi.com/?t=the%20shawshank%20redemption').success(function (data) {
    $scope.mydata = data;
  }
}
 </script>
  </head>
  <body ng-controller="ActivitiesListCtrl">
  <h1>Movie Name</h1>
  <ul>
   <li ng-repeat="data in mydata">
     {{data.Title}}
   </li>
  </ul>
</body>
</html>
</html>

解决方案

There is quite a lot wrong with your code :-( Syntax errors, not injecting $http, the response is a single movie instead of a collection.

Try the following.

<!doctype html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
    <script>
    function ActivitiesListCtrl($scope, $http) {
  $http.get('http://www.omdbapi.com/?t=the%20shawshank%20redemption').success(function (data) {
    $scope.mydata = data;
  });
}
 </script>
  </head>
  <body ng-controller="ActivitiesListCtrl">
  <h1>Movie Name</h1>
    {{mydata.Title}}
</body>
</html>

Example with a collection:

<!doctype html>
<html ng-app>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
    <script>
    function ActivitiesListCtrl($scope, $http) {
  $http.get('http://www.omdbapi.com/?s=True%20Grit').success(function (data) {
    $scope.mydata = data;
  });
}
 </script>
  </head>
  <body ng-controller="ActivitiesListCtrl">
  <h1>Movie Name</h1>
    <ul> 
       <li ng-repeat="data1 in mydata.Search"> {{data1.Title}} </li> 
    </ul>
</body>
</html>

这篇关于在角JS无法获得正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆