这是我的html代码,我在这里调用指令
<div ng-repeat="feat in templateAttributes track by $index">
<md-input-container flex="50">
<feat-directive feat="{{feat}}" />
</md-input-container>
</div>
及以下是自定义指令代码
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" />');
}
}
};
});
但是当我尝试获取feat.DataType
的值时,我在调试代码时得到的值低于undefined
。
$attr
:
Object
feat
:
"{"Name":"DisplayName","DataType":"String","Description":"Display Name","Mandatory":true,"Editable":true,"Extension":false,"MinLength":3,"MaxLength":100,"AllowedValues":"","Value":""}"
__proto__
:
Object
然后我像这样改变代码
link: function(scope, element, attr)
并尝试使用JSON解析器
var feat1 = JSON.parse(attr.feat);
在此更改下面的代码显示输入框中的{{feat.Value}}
<input type="text" id="{{feat.Name}}" value="{{feat.Value}}" ng-model="feat.Value" />
答案 0 :(得分:1)
AngularJS指令创建了自己的scope
,您可以使用parent scope
scope isolation
您可以添加scope
属性作为回报
return {
restrict: 'E',
scope: {
feat: '=feat'
}
...
}