如何将动态变量从javascript分配到angularJS范围

如何将动态变量从javascript分配到angularJS范围,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个表单,用户点击谷歌地图,纬度和经度显示在一个文本框中 function addMarker(location) { document.getElementById('hLat').value = location.lat().toPrecision(8); document.getElementById('hLong').value = location.lng().toPrecision(8); } 文本框定义为 <input type="text" id = "

我有一个表单,用户点击谷歌地图,纬度和经度显示在一个文本框中

function addMarker(location) {
  document.getElementById('hLat').value = location.lat().toPrecision(8);
  document.getElementById('hLong').value = location.lng().toPrecision(8);  
}
文本框定义为

<input type="text"  id = "hLat" name = "hLat" ng-model="form.latitude"/>
<input type="text"  id = "hLong" name = "hLong" ng-model="form.longitude"/>
输出为25.6,函数成功执行

我的控制器定义如下:

var app = angular.module('myApp', []);
app.controller('FormCtrl', function ($scope, $http) {

    $scope.formData = {
        epic: "default"

    };

    $scope.formDetails = {

        latitude: "default",
        longitude: "default"

    };


    $scope.save = function() {
        formData = $scope.form;
    };

        $scope.submitDetails = function() {
        console.log("posting data....");
        $scope.formDetails = $scope.form;
        document.write($scope.formDetails.latitude); // strange behaviour
        $http({method:'GET', url:'http://localhost/nametestjson.php', params:{search_type:"details", latitude:$scope.formDetails.latitude, longitude:$scope.formDetails.longitude, page_no:"1", results_per_page:"15"}}).success(function(data){        
            $scope.friends = data.response.docs;        
            var str = JSON.stringify(data, undefined, 2);       
            alert("success");
        }).error(function(data, status, headers, config) {

            alert(status);
            });           
    };

});

我前一天也有同样的问题

function addMarker(location) {
  document.getElementById('hLat').value = location.lat().toPrecision(8);
  document.getElementById('hLong').value = location.lng().toPrecision(8);  
}
目前您正在使用angularjs,那么您为什么要编写纯javascipt编码呢??请试试这个

function addMarker(location) {
      $scope.form.latitude= location.lat().toPrecision(8);
      $scope.form.longitude= location.lng().toPrecision(8);  
      $scope.$apply();
    }

addMarker
应该是作用域变量的控制器方法谢谢。但我不知道如何添加该函数,因为我的GoogleMapsAPI是从普通JavaScriptAPI加载的,但在将其添加到addMarker时,我得到$scope未定义
function addMarker(location) {
      $scope.form.latitude= location.lat().toPrecision(8);
      $scope.form.longitude= location.lng().toPrecision(8);  
      $scope.$apply();
    }