I have a directive that can be used several times on a page. In the template of this directive, I need to use identifiers for the input element, so that I can "bind" the shortcut to it like this:
<input type="checkbox" id="item1" /><label for="item1">open</label>
Now the problem is that as soon as my directive is included several times, the identifier "item1" is no longer unique, and the label does not work correctly (it should check / uncheck the box when clicked).
How is this problem fixed? Is there a way to assign a "namespace" or "prefix" to the template (for example, asp.net with the ctl00 prefix ...-)? Or I need to include angular -Expression in each id attribute, which consists of a directive identifier from Scope + static identifier. Something like:
<input type="checkbox" id="{{directiveID}} + 'item1'" /><label for="{{directiveID}} + 'item1'">open</label>
Edit:
My directive
module.directive('myDirective', function () { return { restrict: 'E', scope: true, templateUrl: 'partials/_myDirective.html', controller: ['$scope', '$element', '$attrs', function ($scope, $element, $attrs) { ... }
My html
<div class="myDirective"> <input type="checkbox" id="item1" /><label for="item1">open</label> </div>
angularjs uniqueidentifier templates angularjs-directive
NoRyb Jan 09 '14 at 13:44 on 2014-01-09 13:44
source share