사용자가 버튼을 클릭 할 때 동적으로 추가하려는 지시문에 몇 가지 문제가 있습니다. 이것을 달성하기위한 최선의 방법은 무엇입니까?AngularJS에 동적으로 지시문을 추가하십시오.
내 연구에서 element.bind
(jqLite 출신)을 찾았지만 인터넷에서 많은 예제를 찾지 못했습니다. 사전에
.directive('addattributes', function() {
return {
restrict: 'E',
scope: {
attributes: '=options',
attr: '='
},
template:
'<div ng-class="classInput">' +
' <div class="col-md-8" style="padding-left: 0;">' +
' <label>Nome do Atributo</label>' +
' <input type="text" class="form-control" placeholder="Attrs name" ng-model="attrname" ng-change="validation()">' +
' </div>' +
' <div class="col-md-4" style="padding-right: 0;"> ' +
' <label>Tipo do Atributo</label> ' +
' <select class="form-control" ng-options="attribute.name for attribute in attributes" ng-model="attrtype"></select>' +
' </div> '+
' <div class="clearfix"> '+
' <div> '+
' <button type="button" class="btn btn-default btn-xs pull-right" ng-click="changeButton()" style="margin-top: 1em;" ng-show="showBtn == true"> Adicionar </button> ' +
' </div> {{attr}}'+
'</div>',
link: function(scope,element,attrs){
scope.showBtn = true;
scope.classInput = 'form-group';
scope.attrtype= scope.attributes[2];
scope.changeButton = function(){
scope.showBtn = false;
scope.attr = {
name: scope.attrname,
type: scope.attrtype
};
}
scope.validation = function(){
if(scope.attrname.length < 6) {
scope.classInput = 'form-group has-error';
} else {
scope.classInput = 'form-group has-success';
}
}
}
};})
감사합니다 좋은 하루
HTML 코드
<div class="form-group">
<button class="btn btn-info" ng-click="addAttr()">Add Atributtes</button>
</div>
<addattributes options="attributes" attr="obj"></addattributes>
AngularJS와 지시했다 :
여기 내 코드입니다!
'addAttr() '함수가 무엇을합니까 ? –
아직 아무것도, 그 기능을 html을 추가 할 것이라고 :) –
나는 그것을 읽을 수 있지만 컨트롤러에서 DOM을 조작 안티 패턴입니다. –