【发布时间】:2018-09-17 20:50:44
【问题描述】:
下面我的控制器通过如下服务返回一个json:
.controller('googleFormsCtrl', function ($scope, $http, $window, Userr,$timeout,$location) {
// SEND THE HTTP REQUEST
var url;
var externalWindow;
// MAKE API REQUEST for Authorization
$http.get('/api/googleAuth').then(function (response) {
url = response.data;
});
this.googleAuthentication = function () {
externalWindow = $window.open(url, "Please Sign in with Google", "width:350px,height:350px");
};
// Close Authorization and set credentials
window.onmessage = function (info) {
externalWindow.close();
// Get the URL
var urlCode = info.data;
// Get pure code of user
var idx = urlCode.lastIndexOf("code=");
var code = urlCode.substring(idx + 5).replace("#", "");
// GET ALL FORMS
Userr.getCredentials(code).then(function (res) {
$scope.forms = JSON.stringify(res.data);
});
};
})
如上所示,我在请求的回调中获取了所有表单,我需要使用它们才能将它们放入另一个页面的选项菜单中,如下所示:
<div>
<h2> Google Forms </h2>
</div>
<br>
<br>
<br>
<div id="selectForm">
<div class="form-group">
<br>
<!--The Forms should be displayed below dynamically-->
<h5>Select Google Form</h5>
<select class = "form-control" ng-model="model.id"
ng-options="form.id as form.name for form in forms" >
</select>
{{model.id}}
</div>
<button class="btn btn-default" type="submit">OK</button>
</div>
这就是为什么我必须在函数范围之外访问它们。我来自 res.data 的数据是这样的:
[{
"id": "1o3FUAJAPsS2m93_ECkVtLcYbMaaWHk6UGHIFG-6lpyA",
"name": "Normal Form"
}, {
"id": "1QB5MHWtXytLUqGHUH1Ac-V9fS7hHlCqSWXjq2iFz1Zk",
"name": "Parti Davetiyesi"
}];
如何将这些数据导出到函数范围之外?感谢任何帮助或提示。提前致谢。
【问题讨论】:
-
代码为什么要对数据对象进行字符串化?
ng-option指令接受一个对象而不是字符串。
标签: angularjs model-view-controller scope mean-stack