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

  • AFISHAL

    AFISHAL

    7 Mart 2009
  • Rachel Talbott

    Rachel Talbo

    26 Ocak 2011
  • UCBerkeley

    UCBerkeley

    3 Mayıs 2006