2014-03-31 122 views
0

我可以傳遞字符串和對象,但由於某些原因,當我將回調函數添加到我作爲可配置選項使用的作用域對象時,Angular將它們取出。如何將函數作爲參數傳遞給AngularJS指令?

HTML:

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

JS:

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(); 
      }; 
     }] 
    }; 
}]); 

這裏是一個小提琴:http://jsfiddle.net/revolunet/pHZNY/

感謝您的幫助。

+0

作爲一個更新,我發現,如果我' 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插件的方式。如果我在指令中插入一個對象,它就會起作用。如果我使用$ scope模型並使用表達式將該值與HTML中的指令相關聯,則它不起作用。我不知道爲什麼Angular剝離了$ scope模型中的函數。 –

相關問題