SORU
2 HAZİRAN 2013, Pazar


Nasıl bir yöntem AngularJS bir yönergede tanımlanan aramak?

İşte kod bir Direktif var,:

.directive('map', function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<div></div>',
        link: function($scope, element, attrs) {

            var center = new google.maps.LatLng(50.1, 14.4); 
            $scope.map_options = {
                zoom: 14,
                center: center,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            // create map
            var map = new google.maps.Map(document.getElementById(attrs.id), $scope.map_options);
            var dirService= new google.maps.DirectionsService();
            var dirRenderer= new google.maps.DirectionsRenderer()

            var showDirections = function(dirResult, dirStatus) {
                if (dirStatus != google.maps.DirectionsStatus.OK) {
                    alert('Directions failed: '   dirStatus);
                    return;
                  }
                  // Show directions
                dirRenderer.setMap(map);
                //$scope.dirRenderer.setPanel(Demo.dirContainer);
                dirRenderer.setDirections(dirResult);
            };

            // Watch
            var updateMap = function(){
                dirService.route($scope.dirRequest, showDirections); 
            };    
            $scope.$watch('dirRequest.origin', updateMap);

            google.maps.event.addListener(map, 'zoom_changed', function() {
                $scope.map_options.zoom = map.getZoom();
              });

            dirService.route($scope.dirRequest, showDirections);  
        }
    }
})

Kullanıcı bir eylem updateMap() seslenmek istiyorum. Eylem düğmesi Direktif değildir.

Bir kumanda updateMap() aramak için en iyi yolu nedir?

CEVAP
28 AĞUSTOS 2013, ÇARŞAMBA


Eğer izole kapsamları kullanmak istiyorsanız bir kontrol nesnesi iki yönlü bağlama kullanarak iletebilirsiniz ( '' ) değişken = denetleyicisi kapsamı. Bu şekilde ayrıca bir sayfada aynı Direktifi birkaç örneği kontrol edebilirsiniz.

Başvuru için short plunker example Bir ben yarattı

Denetleyici/Yönetmeliği:

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

app.controller('MainCtrl', function($scope) {
  $scope.focusinControl = {
  };
});

app.directive('focusin', function factory() {
  return {
    restrict: 'E',
    replace: true,
    template: '<div>A:{{internalControl}}</div>',
    scope: {
      control: '='
    },
    link      : function (scope, element, attrs) {
      scope.internalControl = scope.control || {};
      scope.internalControl.takenTablets = 0;
      scope.internalControl.takeTablet = function() {
        scope.internalControl.takenTablets  = 1;  
      }
    }
  };
});

HTML:

<button ng-click="focusinControl.takeTablet()">Call directive function</button>
<h4>In controller scope:</h4>
{{focusinControl}}
<h4>In directive scope:</h4>
<focusin control="focusinControl"></focusin>
<h4>Without control object:</h4>
<focusin></focusin>

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Mismag822 - The Card Trick Teacher

    Mismag822 -

    18 EKİM 2008
  • RobertDuskin

    RobertDuskin

    12 HAZİRAN 2008
  • RomanAtwood

    RomanAtwood

    18 Kasım 2009