简体   繁体   English

如何将ng模型传递给控制器​​函数,该函数调用GET请求到nodeJS服务器?

[英]How to pass an ng-model to a controller function which calls a GET request to the nodeJS server?

Here's what I'm trying to do: 这是我想做的事情:

  1. Pass a string from a text input with an ng-model bound to it 从绑定了ng模型的文本输入中传递字符串
  2. to a function in the controller which 到控制器中的功能
  3. calls a GET request in the service which 在服务中调用GET请求
  4. gets returned by the node server 由节点服务器返回

Below is what I've tried so far, which has given me the following error: 以下是到目前为止我尝试过的操作,它给了我以下错误:

Uncaught object angular.js:78

Main.html (1): Main.html(1):

<div class="input-group input-group-lg">
    <input ng-model="businessName" type="text" class="form-control" placeholder="Business Name">
</div>

google.js (2): google.js(2):

var findGPlace2 = function() {
  GoogleService.findPlace2($scope.businessName).then(function(data) {
    $scope.gPlace2 = data;
  });
}
findGPlace2();

google-service.js (3): google-service.js(3):

this.findPlace2 = function(biz){
    var deferred = $q.defer();
    var obj = {
      business: biz
    };
    $http(
      {
        method: 'GET', 
        url: 'http://localhost:12200/find-google-place-2', 
        data: biz
      }).success(function(data) {
        deferred.resolve(data);
      }).error(function(err) {
      deferred.reject(err);
    });
    return deferred.promise;
  };

server.js (4): server.js(4):

//find google place 2
app.get('/find-google-place-2', function(req, res) {
  request('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + req.data.biz + '&key=AIzaSyBvakIQ68QV2', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    res.send(body);
  }
})
});

This is a very difficult problem to debug since the error isn't giving me a line in my code. 这是一个很难调试的问题,因为该错误并未在代码中给我一行。 Any help would be greatly appreciated. 任何帮助将不胜感激。

================================================== ==================================================

================================================== ==================================================

UPDATE: I removed ng-autocomplete from the dependencies, and the Uncaught object error is gone and my angular code is rendering now, but this error came up: 更新:我从依赖项中删除了ng-autocomplete, Uncaught object错误消失了,我的角度代码现在正在渲染,但是出现了这个错误:

GET http://localhost:12200/find-google-place-2 500 (Internal Server Error) angular.js:8380
    (anonymous function) angular.js:8380
    sendReq angular.js:8180
    $http.serverRequest angular.js:7921

I'm sure it has to do with how I'm passing my req data (the string from the text input), here is the function: 我确定这与我传递请求数据(来自文本输入的字符串)的方式有关,这里是函数:

this.findPlace2 = function(biz){
    var deferred = $q.defer();
    var obj = {
      business: biz
    };
    $http(
      {
        method: 'GET', 
        url: 'http://localhost:12200/find-google-place-2', 
        data: biz
      }).success(function(data) {
        deferred.resolve(data);
      }).error(function(err) {
      deferred.reject(err);
    });
    return deferred.promise;
  };

Here's where it's actually running in my server.js file: 这是我的server.js文件中实际运行的位置:

app.get('/find-google-place-2', function(req, res) {
  request('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + req.data.biz + '&key=AIzaSyBvakIQ68QV2', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    //console.log(body) 
    res.send(body);
  }
})
});

There isn't really enough to go on with what you've given me, but here are some suggestions on how to go about debugging: 您给我的东西还远远不够,但是这里有一些有关如何进行调试的建议:

  1. Is this problem happening before or after the http request? 是在HTTP请求之前还是之后发生此问题? Use a console.log on your server to find out. 使用服务器上的console.log进行查找。
  2. Use chrome dev tools or firebug and set breakpoints in your code to see how far you go. 使用chrome开发工具或firebug,并在代码中设置断点,以了解您的发展。

This is almost definitely (my guess) an error in passing in the required dependencies to the module (ngRoute the most common, but anything else that is required will throw that uncaught object error): 在我将所需的依赖项传递给模块时,这几乎绝对是(我的猜测)错误(最常见的是ngRoute,但是其他任何必需的操作都会引发该未捕获的对象错误):

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

If that is not a simple fix for you, a way to get an actual debug message easily is to use an unminified source for angular, and run it in Chrome Canary build. 如果这对您来说不是一个简单的解决方法,则轻松获取实际调试消息的方法是使用未缩小的角度源,然后在Chrome Canary版本中运行它。

http://www.google.com/intl/en/chrome/browser/canary.html http://www.google.com/intl/zh-CN/chrome/browser/canary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM