부모 지시문 컨트롤러를 통해 서로 통신하는 두 개의 사용자 지정 지시문, 하나의 parent 지시문 및 하나의 child 지시문을 만들었습니다.지시문이 내가 원하는대로 작동하지 않습니다. - AngularJS
학부모 지침
app.directive('attrsCtrl', function ($compile) {
return {
restrict: 'E',
scope: {
attributes: '=array',
options: '='
},
controller: function ($scope, $element) {
$scope.attributes = [];
this.returnOptions = function(){
return $scope.options;
}
this.saySomething = function (obj) {
$scope.attributes.push(obj);
alert(obj.name + "/" + obj.type.name);
var newDirective = angular.element('<attributes> </attributes>');
$element.append(newDirective);
$compile(newDirective)($scope);
}
}
}})
아이 지침
app.directive('attributes', function ($compile) {
return {
restrict: 'E',
require: '^attrsCtrl',
template: '<div ng-class="classInput">' +
' <div class="col-md-6" style="padding-left: 0;">' +
' <label>Nome do Atributo</label>' +
' <input type="text" class="form-control input-sm" placeholder="Nome do Atributo" ng-model="attrname" ng-change="validation()" ng-disabled="afterSend">' +
' </div>' +
' <div class="col-md-4 " style="padding-left: 0;"> ' +
' <label>Tipo do Atributo</label> ' +
' <select class="form-control input-sm" ng-options="option.name for option in options" ng-model="attrtype" ng-disabled="afterSend"></select>' +
' </div> ' +
' <div class="col-md-2" style="padding-right: 0;"> ' +
' <label> </label>' +
' <button type="button" class=" btn btn-default pull-right" ng-click="changeButton()" style="margin-top: 1em;" ng-disabled="disabled"> Adicionar </button>' +
' </div> ' +
'</div>' +
'<div class="clearfix></div>',
link: function (scope, element, attrs, attrsCtrl) {
scope.classInput = 'form-group';
scope.disabled = true;
scope.afterSend = false;
scope.options = attrsCtrl.returnOptions();
scope.changeButton = function() {
scope.attr = {
name: scope.attrname,
type: scope.attrtype
};
attrsCtrl.saySomething(scope.attr);
scope.disabled = true;
scope.afterSend = true;
}
scope.validation = function() {
if (scope.attrname.length < 6) {
scope.classInput = 'form-group has-error';
} else {
scope.classInput = 'form-group has-success';
scope.disabled = false;
}
}
}
};})
HTML
<attrs-ctrl array="myAttributes" options="options" >
<attributes/>
</attrs-ctrl>
내 문제는 지시문을 만들기 위해 두 번 클릭하면 자동으로 다른 지시문이 만들어 지지만 아무 것도 입력 할 수 없다는 것입니다. 이 동작은 "Adicionar"버튼을 클릭해야만 발생합니다.
관련이없는,하지만 변화는' '폐쇄 태그를 좋아하지 않는 AngularJS와 같은 ''에. –
cgTag
뭘 원하는지 모르겠다 ... 내 예제가 도움이 될 수 있습니다. http : //plnkr.co/edit/ZQmarVcyLWo5Q6iMbVcb? p = info –
** 나는 심각하게 ** 템플릿을 사용하지 않고 templateURL을 사용하는 것이 좋습니다. 지령. –