在HTML的<script>中使用$ scope数据

时间:2019-10-20 16:46:40

标签: javascript html angularjs

我的数据存储在main.js的$ scope.cars中。现在,我了解了如何使用{{cars}}以HTML格式获取此数据。但是,这在“脚本”标签中不起作用。在以下代码中,您可以看到位于main.js中的我的代码。

$http( {
method: "GET",
url : $scope.myUrl,
headers : {'Accept':'application/sparql-results+json', 'Content-Type':'application/sparql-results+json'}
} )
.success(function(data, status ) {
    $scope.cars1 = [];
    $scope.result = data;
    angular.forEach(data.results.bindings, function(val) { 
        $scope.newObject = {};
        $scope.newObject.Brand = val.brand.value;
        $scope.newObject.Fuel = val.fuel.value;
        $scope.newObject.Gearbox = val.gearbox.value;
        $scope.newObject.Price = val.price.value;
        $scope.newObject.Horsepower = val.hp.value;
        $scope.newObject.Mileage = val.mileage.value;
        $scope.newObject.Info = val.info.value;
        $scope.newObject.Year = val.year.value;
        $scope.newObject.Longitude = val.long.value;
        $scope.newObject.Latitude = val.lat.value;
        $scope.cars1.push($scope.newObject);
    });
    $scope.cars = GeoJSON.parse($scope.cars1, {Point: ['Latitude', 'Longitude']});
})

.error(function(error ){
    console.log('Error '+error);
});

有什么方法可以将$ scope.cars中的数据转换为变量(可变汽车)或在脚本中使用$ scope.cars?

我想在index.html中的脚本中使用以下函数中的数据。

  // This adds the data to the map
  map.on('load', function (e) {
      // This is where your '.addLayer()' used to be, instead add only the 
      //source without styling a layer
      map.addSource("places", {
      "type": "geojson",
      "data": cars
   });
   // Initialize the list
   buildLocationList(cars);
});

0 个答案:

没有答案