user1491636
user1491636

Reputation: 2436

Pass variable to angularjs directive

I have an angularjs directive element with an external template (url) and controller.

app.directive('ngMyDirective', function() {
    return {
        restrict : 'E',
        templateUrl : 'my-template.html',
        controller : 'MyController'
    }
});

This directive is currently used in a couple of views. The html it generates contains a textarea which requires a dynamic number of rows depending on parent view of the directive. Is there a way to pass the directive a variable to set the number of rows dynamically and which can be accessed in both the template and controller?

Upvotes: 0

Views: 84

Answers (1)

Rahul Ravindran
Rahul Ravindran

Reputation: 318

You could use the isolate scope syntax and declare a 2 Way binding variable like this scope:{rows:'='} in your directive definition object.This can be used as a normal scope variable in ur link function.to bind this rows to parent directive we will have to create an attribute rows in the directive HTML with value as the scope variable of the parent u want to bind it to For eg

<div ng-controller='parent having       dynamic value totalrows'>
<child-directive rows='totalrows' />
</div>

Upvotes: 2

Related Questions