簡體   English   中英

不使用$ http.get從json-angularjs調用獲取json數據到外部URL

[英]Not getting json data from json-angularjs call to an external url with $http.get

控制器代碼:

var myApp = angular.module('myApp', []);
myApp.controller('myController', function($scope, $http) {
    $http.get('data.json').then(function(data) {
        $scope.artist = data;
    });
});

html代碼:

<div class="main" ng-controller="myController">

    <ul class="artistlist">

        <li class="artist cf" ng-repeat="x in artist">

            <img ng-src="images/{{x.shortname}}_tn.jpg" alt="Photo of {{x.name}}">

            <div class="info">

                <h2>{{x.name}}</h2>

                <h3>{{x.reknown}}</h3>

            </div>

        </li>

    </ul>

</div>

$http服務具有用於獲取詳細信息的data屬性。

數據 – {string | Object} – 用轉換函數轉換的響應主體

像這樣更改您的控制器代碼

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

myApp.controller('myController', function($scope,$http){

    $http.get('data.json').then( function(response){
    $scope.artist = response.data;
    });
});

並使用ng-app="myApp"初始化html

試試這個: 您的控制器

var myApp = angular.module('myApp', [])

myApp.controller("myController", ["$scope", "$http", function ($scope, $http) {
    $http.get("data.json").then(
        function (response) { $scope.artist = response.data },
        function (error) { console.log(error) }
    )
}])

確保檢查瀏覽器的控制台

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM