질문 : 부모 컨트롤러에서 변수 값이 변경되면 구성 요소 템플릿을 변경할 수 있습니까?각도 : 변수를 사용하여 구성 요소 템플릿 변경
var topBarTemplateWithButton = [
'<section id="filters">',
'<div class="pull-left">',
'<h1>{{$ctrl.step}}</h1>',
'<div class="pull-left pageActionContainer">',
'<ng-transclude></ng-transclude>',
'</div>',
'</div>',
'</section>'].join(' ')
var topBarTemplateWithoutButton = [
'<section id="filters">',
'<div class="pull-left">',
'<h1>{{$ctrl.step}}</h1>',
'</div>',
'</section>'].join(' ')
myApp.component('topBar', {
bindings: {
step: '<'
},
template: this.templateToUse,
transclude: true,
controller: function() {
var me = this;
me.$onInit = function() {
this.templateToUse = topBarTemplateWithButton;
}
me.$onChanges = function(changes) {
if(changes.step.currentValue != "Projects") {
this.templateToUse = this.topBarTemplateWithoutButton;
}else {
this.templateToUse = topBarTemplateWithButton;
}
}
}
});
및
이var topBarTemplateWithButton = [
'<section id="filters">',
'<div class="pull-left">',
'<h1>{{$ctrl.step}}</h1>',
'<div class="pull-left pageActionContainer">',
'<ng-transclude></ng-transclude>',
'</div>',
'</div>',
'</section>'].join(' ')
var topBarTemplateWithoutButton = [
'<section id="filters">',
'<div class="pull-left">',
'<h1>{{$ctrl.step}}</h1>',
'</div>',
'</section>'].join(' ')
myApp.component('topBar', {
bindings: {
step: '<'
},
template: '<div ng-include="$ctrl.templateToUse"/>,
transclude: true,
controller: function() {
var me = this;
me.$onInit = function() {
this.templateToUse = topBarTemplateWithButton;
}
me.$onChanges = function(changes) {
if(changes.step.currentValue != "Projects") {;
this.templateToUse = this.topBarTemplateWithoutButton;
}else {
this.templateToUse = topBarTemplateWithButton;
}
}
}
});
이 두 개의 예제가 작동하지 않습니다 는 여기에 내가 따르려고 두 가지 방법이 있습니다. 따라서 step
값이 변경되면이 구성 요소의 템플릿을 변경할 수 있습니까? 도와 줘서 고마워.
부모 컨트롤러에 값을 전달해야 할 필요가 있으며 'ng-if', ng-show', ng-hide' 등과 같은 템플릿 지시문을 사용하여 구성 요소 – theaccordance
'ng-if' 지시자는 어떻게하는지에 대한 가장 좋은 예입니다. 즉석에서 HTML을 변경하려면 감시자를 등록 취소하고 범위를 삭제해야하며 새 HTML에 새로운 범위가 필요하며 지시문을 컴파일하고 링크해야합니다. 어떻게 수행되는지 보려면 : [ng-if source code] (https://github.com/angular/angular.js/blob/master/src/ng/directive/ngIf.js). 아니면 [ng-if 지시어] (https://docs.angularjs.org/api/ng/directive/ngIf)를 사용하십시오. – georgeawg
@theaccordance 감사합니다. – br1