Javascript Can';t访问HTML中的嵌套JSON对象

Javascript Can';t访问HTML中的嵌套JSON对象,javascript,html,angularjs,json,Javascript,Html,Angularjs,Json,AngularJS V1.6.4 $scope.aCourse[“name”]已正确登录到控制台,但在HTML代码中,屏幕中没有填充任何内容 $scope.getCourse = function(idd){ $http.defaults.headers.common['Authorization'] = 'Basic ' + btoa($cookieStore.get('username') + ':' + $cookieStore.get('password') );

AngularJS V1.6.4

$scope.aCourse[“name”]已正确登录到控制台,但在HTML代码中,屏幕中没有填充任何内容

$scope.getCourse = function(idd){
        $http.defaults.headers.common['Authorization'] = 'Basic ' + btoa($cookieStore.get('username') + ':' + $cookieStore.get('password')  );
        $http({
              method: 'GET',
              url: 'http://localhost:8080/course/'+idd,

            }).then(function successCallback(response) {
                $scope.aCourse = response.data;
                console.log($scope.aCourse["name"]);

                window.location = "/website-take-course.html";
              }, function errorCallback(response) {
                  alert("Course data in fetching failed");
              });
    }
HTML代码:

<div class="page-section padding-top-none" ng-repeat="c in aCourse" >
            <div class="media media-grid v-middle">
              <div class="media-left">
                <span class="icon-block half bg-blue-300 text-white">1</span>
              </div>
              <div class="media-body" >
                <h1 class="text-display-1 margin-none" >{{c.name}}</h1>
              </div>
            </div>
            <br/>
            <p class="text-body-2">{{c.description}}</p>
          </div>

1.
{{c.name}}

{{c.description}


在角度代码中,您正在设置响应数据的路线。然后,您可以通过以下方式作为对象访问数据:

$scope.aCourse["name"]
然后,在html中,您在$scope.aCourse上运行ng repeat,就像它是一个对象数组一样:

<div class="page-section padding-top-none" ng-repeat="c in aCourse" >


您可能需要将aCourse设置为一个对象数组以使用当前html,或者更新html并使用
aCourse.name
aCourse.description

访问aCourse中的对象。根据您的帖子,它看起来像$scope.aCourse是一个对象,而不是数组

修改如下:

<div class="page-section padding-top-none" ">
    <div class="media media-grid v-middle">
        <div class="media-left">
            <span class="icon-block half bg-blue-300 text-white">1</span>
        </div>
        <div class="media-body">
            <h1 class="text-display-1 margin-none">{ aCourse.name }}</h1>
        </div>
    </div>
    <br/>
    <p class="text-body-2">{{aCourse.description}}</p>
</div>

文件。写(“”);
{{key}}:{{value}

可能有两种情况:

1.
$scope.aCourse
是一个对象数组
[{},{},{}]

演示

var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl',函数($scope){
$scope.a课程=[
{
“名称”:“阿尔法”,
“说明”:“说明1”
},
{
“名称”:“测试版”,
“说明”:“说明2”
},
{
“名称”:“伽马”,
“说明”:“说明3”
}
];
});

{{c.name}}

{{c.description}


发布您在console.log$scope.aCourse中看到的json。查看开发人员工具-控制台中的错误,或查看网络选项卡中返回的响应。为什么要迭代对象(
ng repeat=“c in aCourse”
)这是错误的。相反,请尝试
{{aCourse.name}
您是否检查了答案?为什么投票被否决?似乎是正确的答案谁能告诉我为什么他们否决了这个?据我所知,我给出了问题的准确信息和解决方案。当数据是对象时,可以使用
ng repeat=“(键,值)in data”
。请解决这个问题。MazenMohamed是你的问题,而不是别人的问题answers@OmriLuzon我应该解释一下better@Sajeetharan当我尝试使用您的演示中包含的json对象时,它只有在我的“http”请求中的“.then”函数之外定义时才起作用,如果我将它放在“.then”函数中函数它不工作。@MazenMohamed它与此无关,请检查您是否接收到promise中的数据
 <div ng-repeat="(key,value) in aCourse">
      {{key}} : {{value}}
  </div>