Javascript 使用coffeescript自动完成放置后,无法从getPlace获取值

Javascript 使用coffeescript自动完成放置后,无法从getPlace获取值,javascript,jquery,angularjs,google-maps,coffeescript,Javascript,Jquery,Angularjs,Google Maps,Coffeescript,我是angularJs/coffeescript的新手,正在尝试将其与谷歌地图集成 目标是在使用getPlace()自动完成后获取place_id,并通过将其传递给服务方法将其存储在安全的地方,以便以后保存 这是密码 咖啡代码 class SomeController constructor: (@$scope, @$log, @$location, @SomeService) -> @listing = {} getPlace: (place) ->

我是angularJs/coffeescript的新手,正在尝试将其与谷歌地图集成

目标是在使用getPlace()自动完成后获取place_id,并通过将其传递给服务方法将其存储在安全的地方,以便以后保存

这是密码

咖啡代码

class SomeController

constructor: (@$scope, @$log, @$location, @SomeService) ->

    @listing = {}


    getPlace: (place) ->
          place = @getPlace()
          alert(place.place_id)
          return place

    doSomethingMore: () ->

          @variable.whereGeo =  @getPlace().place_id
          alert(@listing.whereGeo)

    @SomeService.someFunction(@listing)  //save the place id somewhere 

controllersModule.controller('SomeController', SomeController)
HTML


理想情况下,在自动完成后,此处应该出现一些内容

place_id={{sc.place}}
提交
现在使用的模块是ngMap。这里是链接

现在让我补充细节和问题

  • 自动完成工作正常
  • 调用getPlace函数
  • 将按预期使用place_id引发警报。但是,我不知道 把数据放回原处

  • 按submit时,我得到的错误是this.getPlace是 未定义

  • 我尝试过的事情。我在getPlace中使用了fat arrow=>并得到了这个结果。getPlace是未定义的错误,调用getPlace时也不会引发警报

    尝试查找并执行解决方案,但没有成功。 也有助于理解场景,但没有帮助


    非常感谢您对此提供的任何帮助。提前谢谢。

    好的。。所以我做错了。经过一些研究发现了这一点。我把望远镜全弄错了。最后解释一下

    咖啡脚本

    class SomeController
    
    constructor: (@$scope, @$log, @$location, @SomeService) ->
    
    @listing = {}
    @place
        autocomplete_options = {
          types: ['geocode'] 
        }
    
        autocomplete = new google.maps.places.Autocomplete(document.getElementById('where'), autocomplete_options)
    
        google.maps.event.addListener autocomplete, 'place_changed', =>
            place = autocomplete.getPlace()
            console.log(place.place_id)
            @$scope.$apply =>
                @$scope.place = place.place_id
    
    
    
    
    doSomethingMore: () ->
    
          @liating.whereGeo =  @$scope.place // now place id is available in scope 
          alert(@listing.whereGeo)
    
    @SomeService.someFunction(@listing)  //save the place id somewhere 
    
    controllersModule.controller('SomeController', SomeController)
    
    HTML

    
    提交
    
    解释

  • 为了实现目标,必须将地点id分配给 全局变量,然后在整个范围内可用
  • 必须记住coffescript中=>和->之间的区别。我找到了一些很好的文档
  • 最后,答案是有帮助的

  • 好的。。所以我做错了。经过一些研究发现了这一点。我把望远镜全弄错了。最后解释一下

    咖啡脚本

    class SomeController
    
    constructor: (@$scope, @$log, @$location, @SomeService) ->
    
    @listing = {}
    @place
        autocomplete_options = {
          types: ['geocode'] 
        }
    
        autocomplete = new google.maps.places.Autocomplete(document.getElementById('where'), autocomplete_options)
    
        google.maps.event.addListener autocomplete, 'place_changed', =>
            place = autocomplete.getPlace()
            console.log(place.place_id)
            @$scope.$apply =>
                @$scope.place = place.place_id
    
    
    
    
    doSomethingMore: () ->
    
          @liating.whereGeo =  @$scope.place // now place id is available in scope 
          alert(@listing.whereGeo)
    
    @SomeService.someFunction(@listing)  //save the place id somewhere 
    
    controllersModule.controller('SomeController', SomeController)
    
    HTML

    
    提交
    
    解释

  • 为了实现目标,必须将地点id分配给 全局变量,然后在整个范围内可用
  • 必须记住coffescript中=>和->之间的区别。我找到了一些很好的文档
  • 最后,答案是有帮助的
  • <div ng-controller="SomeController as sc">
    
    <div class="col-sm-3">
                <input type="text" class="form-control" name="where" id="where" placeholder=""
                       ng-model="sc.search.where">
            </div>
    
    
    <div class="col-sm-offset-4 col-sm-2 col-xs-1 text-right">
                    <button ng-click="sc.doSomethingMore()" class="btn btn-success btn-lg" id="btn_create">Submit</button>
                </div>
     </div>