ng-repeat 得到评论并且不显示 JSON 对象的结果

ng-repeat gets commented and doesn´t show results from a JSON object

我有这段代码,我不知道为什么,但是 ng-repeat 指令被注释掉了,并且没有显示 JSON 对象的结果。我已经使用 toSource() 方法检查对象是否正确传递给“this.paises2”,看起来一切正常。

我已经检查过这里,但没有任何效果:

AngularJS: ng-repeat not displaying JSON

这里是 HTML 代码:

<html ng-app="miModulo">



<head>

    <link href="mis-css.css" rel="stylesheet" type="text/css">

</head>

<body ng-controller="miControlador as vm">

    <button ng-click="vm.buscarEnRegion()">Pulsar</button>



    <ul ng-repeat="elemento in vm.paises">
    <li>

        {{elemento.name}}



    </li>

    </ul>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

    <script src="mijs1.js"></script>

</body>

和Javascript/AngularJS代码:

angular
.module("miModulo",[])
.controller("miControlador",['$http',funcionPrincipal] ); 

function funcionPrincipal($http)
{   
    this.buscarEnRegion=function()
    {
        $http.get("https://restcountries.eu/rest/v1/region/africa").then(function(miRespuesta) 
        {
            this.paises=miRespuesta;

        //  alert(miRespuesta.toSource());

        alert(this.paises.toSource());



        },function(miRespuesta)
        {

            alert("incorrecto");
        }



        ); //then


    }


}

提前致谢。

改变这个

<ul ng-repeat="elemento in vm.paises">
    <li>
    {{elemento.name}}
    </li>
</ul>

至此

<ul >
    <li ng-repeat="elemento in vm.paises">
    {{elemento.name}}
   </li>
</ul>

几个问题。来自 angular docs

The response object has these properties:

  • data – {string|Object} – The response body transformed with the
  • transform functions. status – {number} – HTTP status code of the
  • response. headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate
  • the request. statusText – {string} – HTTP status text of the response.
  • xhrStatus – {string} – Status of the XMLHttpRequest (complete, error, timeout or abort).

所以你想要的是miRespuesta.data

另一个问题是当您执行 this.paises=miRespuesta 时,this 指的是功能,而不是控制器。所以你需要给控制器分配一个变量。因此,将您的控制器更改为:

function funcionPrincipal($http)
{   
    var vm = this;
    vm.buscarEnRegion=function()
    {
        $http.get("https://restcountries.eu/rest/v1/region/africa").then(function(miRespuesta) 
        {
            vm.paises=miRespuesta.data;
        },function(miRespuesta)
        {
            alert("incorrecto");
        }
        ); //then
    }
}

plunker