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

  • 3DS Max Tutorials

    3DS Max Tuto

    4 AĞUSTOS 2013
  • Dogbert files

    Dogbert file

    12 Ocak 2012
  • nemoking of kinges

    nemoking of

    4 EYLÜL 2009