Michiel
Michiel

Reputation: 4260

Angular, read attribute from directive tag

I have a directive that looks like this:

<div class="slidesShow" interval="5" ng-controller="slideshowController" ng-init="init()">
    <img ng-repeat='item in slides' src="{{item.src}}" alt="{{item.alt}}" />
</div>

Can I get the value of the interval attribute in my controller function?

I have tried this:

angular.module('awsApp').controller('slideshowController', function ($scope, $http) {

    $scope.init = function () {
        console.log('unterval:' + $scope.attr.interval);
    };

});

This gives ne the error: Error: $scope.attr is undefined

Can I acces this attribute? And if so, How?

Upvotes: 0

Views: 53

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

You can inject attrs into the controller like

angular.module('my-app').controller('slideshowController', function ($scope, $http, $attrs) {
    console.log($attrs)

    $scope.init = function () {
        console.log('unterval:' + $attrs.interval);
    };

});

Demo: Fiddle

Upvotes: 2

Related Questions