在 Angular js 中从 Go 模板获取数据
Getting data from Go template in Angular js
我是 Angular Js 的新手。我设法在 angular js 中从 golang 接收数据。但它在警告框中使用时会给出输出 [object Object] 。我尝试将golang的分隔符从{{ }}改成<<< >>>,但是问题没有解决
Go代码:(我用的是beego)
func (receiver *AdsController) LoadNewCampaignPage() {
view := viewmodels.NewCampaignPageViewModel{}
view.Title = "New Campaign"
receiver.Data["vm"] = view
receiver.Layout = "layouts/ads_header.html"
receiver.TplName = "templates/ads_add_campaign.html"
}
结构viewmodels.NewCampaignPageViewModel{}
type NewCampaignPageViewModel struct {
Title string
ProfileName string
ProfilePicture string
UnUsedBoxes []models.Box
ErrorMessage string
}
Html
<div ng-controller="AddBoxForAdsCtrl">
<button class="_button _button-3" ng-click="showHiddenForm()">Add Box</button>
</div>
JS
var addBoxForAds = angular.module('addBoxForAds', []);
addBoxForAds.controller('AddBoxForAdsCtrl', function ($scope, $http){
var title = $http.get('<<<.vm.Title>>>'); //Data from GO; delimiters are changed.
alert(title);
});
我在这里犯了哪些错误?如何在 angularjs 中从 golang 获取数据?如何使用结构元素UnUsedBoxes(这是一个结构数组)?
$http.get
向服务器发出获取请求以获取 json 数据,您只是将值直接传递给 js 代码。如果我猜对了,您需要将代码更改为
var title = '<<<.vm.Title>>>';
或者,您可以像在这个虚拟示例中那样在 Go 中创建函数(在 beego 框架上可能看起来不同):
import (
"net/http"
"encoding/json"
)
func main() {
http.HandleFunc("/title", title_handler)
http.ListenAndServe(":8000", nil)
}
func title_handler(w http.ResponseWriter, r *http.Request) {
title := map[string]string{"title": "My Title"}
// this should work too:
// title := struct {title string} {"My Title"}
json_title, _ := json.Marshal(title)
w.Header().Set("Content-Type", "application/json")
w.Write(json_title)
}
在 js 中:
var title = $http.get('/title');
我是 Angular Js 的新手。我设法在 angular js 中从 golang 接收数据。但它在警告框中使用时会给出输出 [object Object] 。我尝试将golang的分隔符从{{ }}改成<<< >>>,但是问题没有解决
Go代码:(我用的是beego)
func (receiver *AdsController) LoadNewCampaignPage() {
view := viewmodels.NewCampaignPageViewModel{}
view.Title = "New Campaign"
receiver.Data["vm"] = view
receiver.Layout = "layouts/ads_header.html"
receiver.TplName = "templates/ads_add_campaign.html"
}
结构viewmodels.NewCampaignPageViewModel{}
type NewCampaignPageViewModel struct {
Title string
ProfileName string
ProfilePicture string
UnUsedBoxes []models.Box
ErrorMessage string
}
Html
<div ng-controller="AddBoxForAdsCtrl">
<button class="_button _button-3" ng-click="showHiddenForm()">Add Box</button>
</div>
JS
var addBoxForAds = angular.module('addBoxForAds', []);
addBoxForAds.controller('AddBoxForAdsCtrl', function ($scope, $http){
var title = $http.get('<<<.vm.Title>>>'); //Data from GO; delimiters are changed.
alert(title);
});
我在这里犯了哪些错误?如何在 angularjs 中从 golang 获取数据?如何使用结构元素UnUsedBoxes(这是一个结构数组)?
$http.get
向服务器发出获取请求以获取 json 数据,您只是将值直接传递给 js 代码。如果我猜对了,您需要将代码更改为
var title = '<<<.vm.Title>>>';
或者,您可以像在这个虚拟示例中那样在 Go 中创建函数(在 beego 框架上可能看起来不同):
import (
"net/http"
"encoding/json"
)
func main() {
http.HandleFunc("/title", title_handler)
http.ListenAndServe(":8000", nil)
}
func title_handler(w http.ResponseWriter, r *http.Request) {
title := map[string]string{"title": "My Title"}
// this should work too:
// title := struct {title string} {"My Title"}
json_title, _ := json.Marshal(title)
w.Header().Set("Content-Type", "application/json")
w.Write(json_title)
}
在 js 中:
var title = $http.get('/title');