【问题标题】:Display a JSON field with AngularJS from Golang REST service使用来自 Golang REST 服务的 AngularJS 显示 JSON 字段
【发布时间】:2014-04-22 04:12:23
【问题描述】:

在服务器上,我有以下简单的 Go REST 功能:

func GetFoo(w http.ResponseWriter, r *http.Request){

res1D := &Response1{
    Page:   101,
    Fruits: []string{"apple", "peach", "pear"},
}
res1B, _ := json.Marshal(res1D)
w.Header().Set("Content-Type", "text/json; charset=utf-8")
w.Write(res1B)

}

type Response1 struct {
Page   int
Fruits []string
}

在我的 index.html 上,我的 AngularJS 代码是:

<div ng-controller="SecondCtrl">
{{allFoo}}
<div ng-repeat="foo in allFoos">
    {{foo}}
</div>

还有我的 AngularJS 控制器:

function SecondCtrl($scope, Restangular){

var foos = Restangular.all('rest/foos');

foos.getList().then(function(foo) {
    $scope.allFoos = foo;
});
};

当 index.html 被渲染时,对于 {{allFoos}} 我看到了:

{"0":101,"1":["apple","peach","pear"],"Page":101,"Fruits":["apple","peach","pear"],"route":"rest/foos","parentResource":null,"restangularCollection":true}

对于重复的 AngularJS div,对于 {{foo}},我得到:

101
["apple","peach","pear"]
["apple","peach","pear"]
101
true
rest/foos

我的目标是只显示 Response1 的“页面”字段。 我在重复 div 中尝试了 {{foo.Page}},但随后重复 div 没有显示任何内容,并且我没有看到错误。

【问题讨论】:

    标签: json angularjs go


    【解决方案1】:

    使用{{allFoo.Page}}。您的ng-repeat 循环遍历allFoos 的值,这就是为什么您会在该列表中看到Page 的值101

    【讨论】:

    • 我现在只得到'Page'字段,谢谢!.....你知道为什么我在重复区域中得到多行,应该只有一个 Foo,因此只有一个“页面”?
    • 我错了,这不起作用,根据我看到的示例,{{foo.Page}} 需要工作。我需要找出如何只显示“101”
    猜你喜欢
    • 2012-10-08
    • 2012-12-23
    • 2017-10-11
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多