2014-03-31 3 views
0

문자열과 객체를 전달할 수 있지만 구성 가능한 옵션으로 사용하고있는 범위 객체에 콜백 함수를 추가 할 때 Angular가이를 제거합니다.함수를 매개 변수로 AngularJS 지시문에 전달하려면 어떻게해야합니까?

HTML :

<div ng-controller="DemoCtrl"> 
    scope.options = {{ options }} 
    <br><br> 
    <div my-directive="{{ options }}"></div> 
</div> 

JS : 당신의 도움에 대한 http://jsfiddle.net/revolunet/pHZNY/

감사 : 여기

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

app.controller('DemoCtrl', function($scope) { 
    $scope.options = { 
     test_str: 'test', 
     init_cb: function() { 
      alert('custom init callback'); 
     }, 
     apply_cb: function() { 
      alert('custom apply callback'); 
     } 
    }; 
}); 
app.directive('myDirective', ['$parse', function($parse) { 
    function link(scope, element, attrs) { 
     var options = attrs.myDirective; 
     scope.init(options); 
    } 

    return { 
     restrict: 'A', 
     transclude: true, 
     link: link, 
     controller: ['$scope', function($scope) { 
      $scope.defaults = { 
       init_cb: function() { 
        alert('default init callback'); 
       }, 
       apply_cb: function() { 
        alert('default apply callback'); 
       } 
      }; 
      $scope.settings = {}; 

      $scope.init = function(options) { 
       $scope.settings = $.extend({}, $scope.defaults, $scope.options); 

       // init cb. 
       $scope.settings.init_cb(); 

       // apply cb. 
       $scope.settings.apply_cb(); 
      }; 
     }] 
    }; 
}]); 

는 바이올린입니다.

+0

, 내가 발견 한 그 경우 I' console.log ($ scope.options)''DemoCtrl'에서 obj는 함수를 포함합니다. –

답변

0

방금 ​​범위에 실제 변수를 전달해야 할 때 당신은 단지 문자열이 될 수있는 ATTR를 사용하려고 : 갱신으로

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

app.controller('DemoCtrl', function($scope) { 
    $scope.options = { 
     test_str: 'test', 
     init_cb: function() { 
      alert('custom init callback'); 
     }, 
     apply_cb: function() { 
      alert('custom apply callback'); 
     } 
    }; 
}); 
app.directive('myDirective', ['$parse', function($parse) { 


    return { 
     restrict: 'A', 
     transclude: true, 
     scope: { //use scope to pass in variables 
      options: '=myDirective' 
     }, 

     controller: ['$scope', function($scope) { 


      $scope.defaults = { 
       init_cb: function() { 
        alert('default init callback'); 
       }, 
       apply_cb: function() { 
        alert('default apply callback'); 
       } 
      }; 
      $scope.settings = {}; 

      $scope.init = function(options) { 
       $scope.settings = angular.extend({}, $scope.defaults, $scope.options); 

       // init cb. 
       $scope.settings.init_cb(); 

       // apply cb. 
       $scope.settings.apply_cb(); 
      }(); 
     }] 
    }; 
}]); 
+0

고마워,이 청소기,하지만 난 여전히 'DemoCtrl'의 범위에있는 개체에 메서드를 추가하는 데 문제가 있습니다. 'Console.log ($ scope.options)'를'DemoCtrl'에 추가하면 메소드가 표시됩니다. 그러나 표현식을 HTML의 {{$ scope.options}} 페이지에 인쇄하면 메소드없이 객체를 인쇄합니다. 어떤 이유로 인해 Angular가 그들을 제거하려고합니다 ... –

+0

렌더링하려면 개체를 JSON 문자열로 변환하고 JSON에는 함수가 들어 있지 않습니다. 원래 옵션을 수정할 수 있도록하려면 범위를 false로 설정하십시오. http://jsfiddle.net/pHZNY/8/ – dave

+0

jQuery 플러그인과 비슷한 사용자 정의 콜백 함수로 기본 옵션을 확장하는 쉬운 방법을 만들고자합니다. 지시문에 객체를 삽입하면 작동합니다. $ 범위 모델을 사용하고 표현식을 사용하여 값을 HTML의 지시문에 연결하면 작동하지 않습니다. Angular가 $ scope 모델에서 함수를 제거한 이유를 모르겠습니다. –

관련 문제