How to call a method defined in an AngularJS directive?

前端 未结 13 1302
萌比男神i
萌比男神i 2020-11-22 14:39

I have a directive, here is the code :

.directive(\'map\', function() {
    return {
        restrict: \'E\',
        replace: true,
        template: \'<         


        
13条回答
  •  鱼传尺愫
    2020-11-22 15:09

    You can tell the method name to directive to define which you want to call from controller but without isolate scope,

    angular.module("app", [])
      .directive("palyer", [
        function() {
          return {
            restrict: "A",
            template:'
    ', link: function($scope, element, attr) { if (attr.toPlay) { $scope[attr.toPlay] = function(name) { $scope.text = name + " playing..."; } } } }; } ]) .controller("playerController", ["$scope", function($scope) { $scope.clickPlay = function() { $scope.play('AR Song'); }; } ]);
    .player{
      border:1px solid;
      padding: 10px;
    }
    
    

    Click play button to play

提交回复
热议问题