SORU
22 AĞUSTOS 2013, PERŞEMBE


AngularJS - Yönerge fonksiyon pass

Örnek bir angularJS var

<div ng-controller="testCtrl">

<test color1="color1" updateFn="updateFn()"></test>
</div>
 <script>
  angular.module('dr', [])
.controller("testCtrl", function($scope) {
    $scope.color1 = "color";
    $scope.updateFn = function() {
        alert('123');
    }
})
.directive('test', function() {
    return {
        restrict: 'E',
        scope: {color1: '=',
                updateFn: '&'},
        template: "<button ng-click='updateFn()'>Click</button>",
        replace: true,
        link: function(scope, elm, attrs) { 
        }
    }
});

</script>
</body>

</html>

Ben düğmeye tıkladığınızda bir uyarı kutusu görünür, ama hiçbir şey göstermek istiyorum.

Biri bana yardım edebilir mi?

CEVAP
22 AĞUSTOS 2013, PERŞEMBE


İçeriden bir üst kapsam denetleyici bir işlev kapsamında Direktifi izole Ara, OP dediği gibi HTML dash-separated öznitelik adları kullanın.

Ayrıca eğer fonksiyon bir parametre göndermek istiyorsanız, bir nesne geçirerek: işlevini çağırın

<test color1="color1" update-fn="updateFn(msg)"></test>

JS

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

app.controller("testCtrl", function($scope) {
    $scope.color1 = "color";
    $scope.updateFn = function(msg) {        
        alert(msg);
    }
});

app.directive('test', function() {
    return {
        restrict: 'E',
        scope: {
            color1: '=',
            updateFn: '&'
        },
        // object is passed while making the call
        template: "<button ng-click='updateFn({msg : \"Hello World!\"})'>
            Click</button>",
        replace: true,        
        link: function(scope, elm, attrs) {             
        }
    }
});

Fiddle

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

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CrazyMan

    CrazyMan

    14 Mayıs 2008
  • Matthew Pearce

    Matthew Pear

    9 AĞUSTOS 2009
  • wwjoshdo

    wwjoshdo

    25 Mayıs 2009