SKYnine SKYnine - 1 year ago 154
AngularJS Question

Angularjs - Pass argument to directive

Im wondering if there is a way to pass an argument to a directive?

What I want to do is append a directive from the controller like this:

$scope.title = "title";
$scope.title2 = "title2";


Is it possible to pass an argument at the same time so the content of my directive template could be linked to one scope or another?

here is the directive:

app.directive("directive_name", function(){
return {
template:'<div class="title"><h2>{{title}}</h3></div>',

What if I want to use the same directive but with $scope.title2?

Answer Source

Here is how I solved my problem:


app.directive("directive_name", function(){
    return {
        restrict: 'E',
        transclude: true,
        template: function(elem, attr){
           return '<div><h2>{{'+attr.scope+'}}</h2></div>';
        replace: true


$scope.building = function(data){
    var chart = angular.element(document.createElement('directive_name'));
    chart.attr('scope', data);

I now can use different scopes through the same directive and append them dynamically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download