Here is my html code where i am calling directive
<div ng-repeat="feat in templateAttributes track by $index">
<md-input-container flex="50">
<feat-directive feat="{{feat}}" />
</md-input-container>
</div>
and below is custom directive code
sureApp.directive('featDirective', function () {
alert("Hariom");
return {
restrict: 'E',
template: '<span style="padding-right:20px"><label value="{{feat.Name}}">{{feat.Name}}</label></span>',
link: function(scope, element, feat){
if(feat.DataType === 'Boolean'){
element.append('<input type="text" id="{{feat.Name}}" value="{{feat.Value}}" ng-model="feat.Value" />');
}
else if(feat.AllowedValues && feat.AllowedValues.length > 0){
element.append('<select ng-model="feat.Value" ng-options="x for x in feat.AllowedValues.split(\',\')"></select>');
}
else if(feat.DataType == 'Integer'){
element.append('<input type="text" id="{{feat.Name}}" value="{{feat.Value}}" ng-model="feat.Value" />');
}
else if(feat.DataType == 'String'){
element.append('<input type="text" id="{{attr.feat.Name}}" value="{{attr.feat.Value}}" ng-model="attr.feat.Value" ng-minlength="attr.feat.MinLength" ng-maxlength="attr.feat.MaxLength" />');
}
else if(feat.DataType == 'IpAddress'){
element.append('<input type="text" id="{{feat.Name}}" value="{{feat.Value}}" ng-model="feat.Value" />');
}
}
};
});
But when i am trying to get the value of feat.DataType
i am getting undefined
while I am getting below values when debugging the code.
$attr
:
Object
feat
:
"{"Name":"DisplayName","DataType":"String","Description":"Display Name","Mandatory":true,"Editable":true,"Extension":false,"MinLength":3,"MaxLength":100,"AllowedValues":"","Value":""}"
__proto__
:
Object
Then i change code little bit like this
link: function(scope, element, attr)
and tried used JSON parser
var feat1 = JSON.parse(attr.feat);
After this change below code showing {{feat.Value}}
in inputbox
<input type="text" id="{{feat.Name}}" value="{{feat.Value}}" ng-model="feat.Value" />
link: function(scope, element, feat){
this is wrong, the 3rd argument is for attributes of the element so it should be:link: function(scope, element, attrs){
and thenattrs.feat
– maurycy Feb 13 at 11:10"{"Name":"DisplayName","DataType":"String","Description":"Display Name","Mandatory":true,"Editable":true,"Extension":false,"MinLength":3,"MaxLength":100,"AllowedValues":"","Value":""}"
and when trying toaccess attr.feat.AllowedValues
its coming undefined – Subodh Joshi Feb 13 at 11:13<feat-directive feat="{{feat}}" />
to<feat-directive feat="feat" />
and you should be fine – maurycy Feb 13 at 11:19$attr : Object feat : "feat"
– Subodh Joshi Feb 13 at 11:25