There are some issues with your code snippet:
First you do not assign the assign the attrs to the scope
but to a local variable, so it won't be available in the controller function.
You could try this:
App.directive('datePicker', function(){
return {
scope: {
name : '@'
},
restrict: 'AE',
replace: 'true',
template: '<div class="date"><div class="input-group"><input type="text" class="form-control" id="{{this_name}}" name="{{this_name}}" ng-model="event.{{this_name}}" required/><span class="input-group-addon"><i class="fa fa-calendar"></i></span></div></div>',
controller: ['$scope', function($scope){
$scope.this_name = this_name;
}],
link: function(scope, element, attrs){
scope.this_name = attrs.name;
}
}
});
I do not know if this really works since it is not recommended to use controller and link functions at the same time (taken from the directive guide):
Best Practice: use controller when you want to expose an API to other directives. Otherwise use link.
But according to the docs the attrs are also available as $attrs
to the controller function:
App.directive('datePicker', function(){
return {
scope: {
name : '@'
},
restrict: 'AE',
replace: 'true',
template: '<div class="date"><div class="input-group"><input type="text" class="form-control" id="{{this_name}}" name="{{this_name}}" ng-model="event.{{this_name}}" required/><span class="input-group-addon"><i class="fa fa-calendar"></i></span></div></div>',
controller: ['$scope', '$attrs', function($scope, $attrs){
$scope.this_name = $attrs.name;
}]
}
});
But you have already defined name
in the isolate scope, so it should be availble as scope.name
in the conroller or link function:
App.directive('datePicker', function(){
return {
scope: {
name : '@'
},
restrict: 'AE',
replace: 'true',
template: '<div class="date"><div class="input-group"><input type="text" class="form-control" id="{{this_name}}" name="{{this_name}}" ng-model="event.{{this_name}}" required/><span class="input-group-addon"><i class="fa fa-calendar"></i></span></div></div>',
link: function(scope, element, attrs){
console.log(scope.name); // this is your name defined as attribute name="..." on the tag
}
}
});