Label attribute not working in angular directive?

I am trying to learn how to work with angular directives and so far have been successful. I have only one minor problem that I cannot understand.

In my directive, I got a for attribute for the same input field identifier value. But clicking on the label does not give input focus control, as if it should work fine.

I got this problem in some code example:

<div ng-app='test' ng-controller='testCtrl'>
    <label for="test1">Testlabel 1</label><br>
    <input id="test1" type="text"></input><br>
    <my-input id="test2" 
              label="Testlabel 2" 
              placeholder="enter somthing"
              text="testVar"></my-input><br>
    <span>{{testVar}}</span>
</div>

and javascript:

angular.module('test', [])
.directive('myInput', function() {
    return {
        restrict: 'E',
        template: '<label for={{id}}>{{label}}</label><br>' +
                  '<input id={{id}} type="text" ' +
                  ' placeholder={{placeholder}} ng-model="text" />',
        scope: {
            id: "@",
            label: "@",
            placeholder: "@",
            text: "="
        }
   }
})
.controller('testCtrl', ['$scope', function($scope) {
    $scope.testVar = 'testing';
}]);

Same code in jsfiddle: http://jsfiddle.net/U92em/

What mistake am I making that causes my problem and how to fix it?

+4
source share
1 answer

"" id , . link, :

 link: function(scope,el,attrs){
     el.removeAttr("id");
 }

: http://jsfiddle.net/cherniv/5u4Xp/

compile ( Florent):

 compile: function(el){
     el.removeAttr("id")
 }

: http://jsfiddle.net/cherniv/7GG6k/

+5

All Articles