angularjs。在http.get之后显示对象属性

时间:2014-12-18 23:52:24

标签: javascript angularjs

我是Angular.js的新手,我在Shaping up with angularjs中完成了教程codeschool.com所以请原谅我,如果我想解决的问题可能太容易解决。

所以,我只是试图将$http.get() JSON对象的数据显示在我的文档中。

的index.html:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>League of legends</title>

    <!-- Load StyleSheets -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">

    <!-- Load Javascript Libraries -->
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <script type="text/javascript" src="js/app.js"></script>
</head>
<body ng-app="LeagueOfLegends">
    <!-- Navbar menu -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li>
                        <a href="#">About</a>
                    </li>
                    <li>
                        <a href="#">Contact</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div ng-controller="searchSummoner as summoner">
            <!-- Search Form -->
            <div ng-show="summoner.status.showSearch">
                <form ng-submit="summoner.search()">
                    <div class="well">
                        <div class="form-group">
                            <label for="summonerName">Summoner Name</label>
                            <input type="text" class="form-control" placeholder="Enter Summoner Name" id="summonerName" ng-model="summoner.form.name">
                        </div>
                        <div class="form-group">
                            <label ng-repeat="region in summoner.region">
                                <input type="radio" ng-model="summoner.form.region" ng-value="region">{{region}}
                            </label>
                        </div>
                        <input type="submit" class="btn btn-default" value="Search"></input>
                    </div>
                </form>
            </div>
            <p ng-show="summoner.status.showResult">Get request from: {{summoner.data.name}}</p>
        </div>
    </div>
</body>

app.js(module-controller):

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

app.controller('searchSummoner', ['$http', function($http)
{
    var form = {};
    this.data = {};

    this.form = {};
    this.status = {
        showSearch: true,
        showResult: false
    };

    this.region = ['br', 'eune', 'euw', 'lan', 'las', 'na', 'oce', 'kr', 'tr'];

    this.search = function()
    {
        form = this.form;

        this.form = {};

        // get data from the api rest
        $http.get(getApiUrl(form)).success(function(data){
            this.data = data[form.name];
            console.log(this.data);
        });

        // hide form
        this.status.showSearch = false;
        this.status.showResult = true;
    };
}]);

function getApiUrl(form)
{
    var apiKey = 'fe9eb24f-5800-4f2a-b570-15328062b341';

    return 'https://lan.api.pvp.net/api/lol/' + form.region + '/v1.4/summoner/by-name/' + form.name + '?api_key=fe9eb24f-5800-4f2a-b570-15328062b341'
}

成功制作$http.get之后,我创建一个日志来检查我检索的数据是否是我需要的数据,但它没有在html中显示对象属性

2 个答案:

答案 0 :(得分:1)

您在错误的关闭中使用this。这是你应该做的:

var that = this;//give controller scope access

// get data from the api rest
$http.get(getApiUrl(form)).success(function(data){
    that.data = data[form.name];
    console.log(that.data);
});

答案 1 :(得分:1)

你也可以像这样绑定你的上下文:

$http.get(getApiUrl(form)).success(function(data){
    this.data = data[form.name];
    console.log(this.data);
}.bind(this));

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind