【问题标题】:Cannot run $http in AngularJS plunker [duplicate]无法在 AngularJS plunker 中运行 $http [重复]
【发布时间】:2019-05-04 23:09:58
【问题描述】:

我无法在 plunker 上运行 $http。你能帮我检查一下我的代码吗?

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

QuizApp.controller('QuizController', ['$scope','$http',function($scope,$http) {

  $scope.message = "hey y'all";

  $http.get('questions.json').success(function(data){
    $scope.questions=data
  });

}]);

https://plnkr.co/edit/sJHwt51k4RPKmq5eT2JF?p=preview

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您需要使用 .then 而不是 .success,因为 1.3 以上的 angularjs 版本已弃用它。您还应该使用 .data 来访问实际的 json 响应

    QuizApp.controller('QuizController', ['$scope', '$http', function($scope, $http) {
      $scope.message = "hey y'all";
      $http.get('questions.json').then(function(data) {
        $scope.questions = data.data;
      });
    }]);
    

    PLUNKER DEMO

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 2018-03-18
      • 2017-09-05
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多