此刻刚刚学习了MEAN堆栈,我正在摆弄它,但我有点失落。
我想要实现的是利用外部API自动填充有关最初输入的标记的输入字段。
问题是,如何将foo
的值一直传递给server.js
以进行通话?
tag
并按“获取数据”tag
(在我的.ejs
文件中定义为ng-model="object.tag"
)的值传递给getData
路径server.js
获取tag
并将其传递到外部API网址以调用JSON
文件。以下是一些代码位,以便您了解我的更多内容的结构:
- index.ejs -
<input type="text" ng-model="object.tag" />
<button ng-click="grabFooInfo()">Get Data</button>
- controller.js -
$scope.grabFooInfo = function(){
$http.get('/getData').success(function(res){
// Will do stuff here after data comes back
});
};
- server.js -
app.get('/getData', function (req, res) {
var options = {
host: 'my.api.im.calling.com,
path: '/v1/fooApi/' + // #Need foo here //
headers: {
accept: "application/json"
},
method: 'GET'
};
var req = https.request(options, function (response) {
var fooData = '';
response.on('data', function (data) {
fooData += data;
});
response.on('end', function () {
res.send(fooData);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
});
server.js
中的此内部函数? 答案 0 :(得分:1)
在客户方面, 的 HTML 强>
<input type="text" ng-model="object.tag" />
<button ng-click="grabFooInfo(object)">Get Data</button>
<强> JS 强>
$scope.grabFooInfo = function(object){
$http({
url:'/getData',
method: "GET",
params: object
});
});
在服务器端,
app.get('/getData', function (req, res) {
var data = req.params.data; // it contains the value foo
})
希望这对你有用!!