Angular 1.6 $ http没有错误回复

时间:2017-07-23 09:58:28

标签: javascript angularjs json http

function() {
        "use strict";
        angular.module("App").controller("LabCtrl", ["$rootScope", "$scope", "$state", "$http", function($scope, a, b, $http) {

            a.labs = []; //declare an empty array

            $http({
              method: 'GET',
              url: 'datalab.json'
            }).then(function (response){
                a.labs = response.data;
            },function (error){
                console.log(error);
            });

        }])
    }.call(this),

我的HTML:

<li class="project-item" ng-repeat="lab in labs">   
            <img alt="img title" ng-src="{{lab.img}}"> 
            <span><strong>{{lab.title}}</strong><br>
            {{lab.desc}}</span> 
    </li>

没有跟踪$ index我得到10000个结果?我的.json中只有10个条目。 我的错误日志也不起作用。我得到文件datalab.json的200 OK状态,但该文件名为data / lab.json

lab.json

[
  {
    "title": "Lab #1",
    "desc": "iOS App Design.",
    "img": "http://lorempixel.com/774/600/sports/"
  },
  {
    "title": "Lab #2",
    "desc": "iOS app.",
    "img": "http://lorempixel.com/774/600/abstract/"
  },
  {
    "title": "Lab #3",
    "desc": "App Design.",
    "img": "http://lorempixel.com/774/600/cats/"
  },
  {
    "title": "Lab #4",
    "desc": "Website Design.",
    "img": "http://lorempixel.com/774/600/fashion/"
  },
  {
    "title": "Lab #5",
    "desc": "Illustration.",
    "img": "http://lorempixel.com/774/600/nature/"
  },
  {
    "title": "Lab #6",
    "desc": "Dribbble Concepts",
    "img": "http://lorempixel.com/774/600/technics/"
  },
  {
    "title": "Lab #7",
    "desc": "Mac OS App.",
    "img": "http://lorempixel.com/774/600/transport/"
  },
  {
    "title": "Lab #8",
    "desc": "Website Design.",
    "img": "http://lorempixel.com/774/600/city/"
  },
  {
    "title": "Lab #9",
    "desc": "Website / Dashboard Design.",
    "img": "http://lorempixel.com/774/600/nightlife/"
  },
  {
    "title": "Lab #10",
    "desc": "Website Design.",
    "img": "http://lorempixel.com/774/600/food/"
  }
]

也许我的.htaccess是问题? (必须使用https)我有pushstate链接等.NewHomepage / labor / NewHomepage / kontakt / .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]

1 个答案:

答案 0 :(得分:0)

我想你错过了 - 写了你的控制器,你错过了你的主app模块的依赖关系。而对于使用“$ state”,您需要注入“ngRoute”。 这是您的代码:

    (function() {
        "use strict";
         angular.module("App",[]).controller("LabCtrl", ["$rootScope", "$scope", 
        "$http", function($rootScope,$scope, $http) {

        $scope.labs = []; //declare an empty array

        $http({
          method: 'GET',
          url: 'datalab.json'
        }).then(function (response){

            $scope.labs = response.data;
            // added this log after removing the "track by $index" in 
            // your html
            console.log(response.data.length);
        },function (error){
            console.log(error);
        });

    }])
})();