大家早上好,
我对这个指令有点困惑,我想要的是从 getProperties 函数接收一个 JSON 字符串,例如:
{"class":"someclass","ng-change":"someChange()",ng-click": "someCLick()"}
该指令将创建 JSON 中存在的所有属性(并且它有效),问题是 ng-* 根本不起作用......有什么想法吗?
HTML:
<div ng-repeat="field in fields">
<input type="text" ng-model="ngModel[field.fieldName]" ng-switch="text" property="{{formParams.getProperties(field.fieldName)}}" update-attr />
</div>
这是指令:
.directive('updateAttr', function () {
return {
restrict: 'A',
replace: true,
scope:{
test:'&'
},
terminate:true,
link: function (scope, elem, attrs) {
if (angular.isDefined(attrs['property']) || attrs['property'].lenght != 0) {
var json = JSON.parse(attrs['property']);
elem.removeAttr('property');
angular.forEach(json, function (value, key) {
elem.attr(key, value);
});
}
},
};
})
这是一个 jsFiddle:http://jsfiddle.net/nyyfmd0e/16/