I am trying to make some reusable countdown widgets. Works well with a static content, but when i'm trying to add them on the fly, my directive doesn't understand the variables inside the ngRepeat.
Markups:
<div ng-repeat="cdn in countdowns" class="countdown" countdown-end="{{cdn}}">
<p ng-hide="over">{{days}} jours {{hours}} heures {{minutes}} min {{seconds}} sec</p>
<p ng-show="over">Done</p>
</div>
Directive:
...
link: function(scope, elm, attrs) {
scope.days = '1';
...
}
...
http://jsfiddle.net/hFGb7/14/
Thanks for the replies.
A controller is usually used to contain and maintain the logic for your view, which gets bound to your view via $scope. A directive is something that you might use repeatedly and is called in your view directly through the directive name which you can pass in as an attribute.
AngularJS directives are extended HTML attributes with the prefix ng- . The ng-app directive initializes an AngularJS application. The ng-init directive initializes application data. The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
The ng-app directive defines the root element of an AngularJS application and starts an AngularJS Application. The ng-app directive will auto-bootstrap (automatically initialize) the application when a web page is loaded. It is also used to load various AngularJS modules in AngularJS Application.
Answer:The link option is just a shortcut to setting up a post-link function. controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.
The problem is that the interpolation is not run by the time link function is called. So the value of {{cdn}}
is not available. There are couple of ways of handling this:
cdn
directly in the link function since it is available on the scope. But this will make the directive dependent on the presence of cdn
in the scope.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With