简体   繁体   English

访问rootscope并从参数中增加价值

[英]accessing rootscope and adding value from parameters

I am trying to get the longitude and latitude from the current position and set it to the marker on the google map and it is not working. 我正在尝试从当前位置获取经度和纬度,并将其设置为Google地图上的标记,但它不起作用。 Right now i am not able to access the variable created, I tried doing the navigator in my directive instead but still nothing working. 现在,我无法访问创建的变量,我尝试在指令中执行导航器,但仍然无济于事。

myApp.controller('PhoneListCtrl', ['$rootScope', '$http', '$scope', function ($scope, $http, $rootScope) {

    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
                $scope.$apply(function(){
                $scope.position = position;
          $rootScope.position = position;
        });
        console.log(position);
        $rootScope.latitude = position.coords.latitude;
        $rootScope.longititude = position.coords.longititude;
        console.log(position.coords.latitude);
        console.log(position.coords.longititude);
        });

    }

}]);
myApp.directive("myMaps", ['$rootScope', function($scope, $rootScope){
  console.log( $rootScope.position.coords.latitude + " rootscope ");
  return{
    restrict:'E',
    template:'<div></div>',
    replace: true,
    controller: function($scope, $element,$rootScope) {
    },
    link: function(scope, element, attrs){
      console.log('{{position.coords.latitude}}');
      var myLatLng = new google.maps.LatLng(56, -75.732333);
      var myLatLng2 = new google.maps.LatLng(56.2843691, -70.732333);
      var mapOptions = {
        center: myLatLng,
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
      var marker = new google.maps.Marker({
        position: myLatLng, 
        map: map, 
        title: "hello world!"
      });
      var marker2 = new google.maps.Marker({
        position: myLatLng2, 
        map: map, 
        title: "hello"
      });
      marker.setMap(map);
      marker2.setMap(map);
    }
  };
} ]);

Try reordering the dependencies injected in this line, to match the parameters order: 尝试重新排序此行中注入的依赖项,以匹配参数order:

myApp.controller('PhoneListCtrl', ['$rootScope', '$http', '$scope', function ($scope, $http, $rootScope) {

Fixed line: 固定的线:

myApp.controller('PhoneListCtrl', ['$scope', '$http', '$rootScope', function ($scope, $http, $rootScope) {

Also in the following line, $scope wasn´t injected: 同样在以下行中,未注入$ scope:

myApp.directive("myMaps", ['$rootScope', function($scope, $rootScope){

Fixed line: 固定的线:

myApp.directive("myMaps", ['$scope', '$rootScope', function($scope, $rootScope){

Remember that In this scenario the ordering of the values in the array must match the ordering of the parameters in the controller/directive. 请记住,在这种情况下,数组中值的顺序必须与控制器/指令中参数的顺序匹配。

BTW, $rootScope is not recomended for constantly changing values. 顺便说一句,$ rootScope不推荐用于不断变化的值。 Try using the directives scope. 尝试使用指令范围。 https://docs.angularjs.org/guide/directive https://docs.angularjs.org/guide/directive

I would not suggest using rootScope in this situation. 我不建议在这种情况下使用rootScope You need to wait until angular has evaluated the variable, so trying to console log the var in the directive definition will not work. 您需要等待,直到angular对变量进行了评估,因此尝试在指令定义中控制台记录var无效。

You can pass the controller scope variables to directives. 您可以将控制器作用域变量传递给指令。 See the concept of Isolated Scope: pass some values from the parent scope to the directives 请参阅隔离范围的概念:将某些值从父范围传递给指令

There are 3 types of prefixes AngularJS provides: AngularJS提供了3种前缀:

  1. "@" ( Text binding / one-way binding ) “ @”(文本绑定/单向绑定)
  2. "=" ( Direct model binding / two-way binding ) “ =”(直接模型绑定/双向绑定)
  3. "&" ( Behaviour binding / Method binding ) “&”(行为绑定/方法绑定)

To do it, include the scope variable you want to pass to your directive in html. 为此,请在HTML中包含要传递给指令的作用域变量。

<my-maps position="{{position}}"></my-maps>

Just use $scope as usual in the controller. 只需像往常一样在控制器中使用$scope (I fixed the order of your dependency injection below) (我在下面固定了依赖项注入的顺序)

myApp.controller('PhoneListCtrl', ['$scope', '$http', function ($scope, $http) {
  if(navigator.geolocation){
      navigator.geolocation.getCurrentPosition(function(position){
        $scope.position = position;
      });
  }
}]);

Add the scope definitions in your directive 在指令中添加范围定义

scope: {
    position: "="
}

Full directive: 完整指令:

myApp.directive("myMaps", ['$scope', function($scope){
  return{
    restrict:'E',
    template:'<div></div>',
    replace: true,
    scope: {
      position: "="
    }
    link: function(scope, element, attrs){
      console.log(scope.position);
      var myLatLng = new google.maps.LatLng(56, -75.732333);
      var myLatLng2 = new google.maps.LatLng(56.2843691, -70.732333);
      var mapOptions = {
        center: myLatLng,
        zoom: 5,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
      var marker = new google.maps.Marker({
        position: myLatLng, 
        map: map, 
        title: "hello world!"
      });
      var marker2 = new google.maps.Marker({
        position: myLatLng2, 
        map: map, 
        title: "hello"
      });
      marker.setMap(map);
      marker2.setMap(map);
    }
  };
} ]);

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

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