컨트롤러에서 지시문에 배열을 전달하려고합니다. 컨트롤러에서 배열 값이 업데이트 될 때 그 이유가 반영됩니다 지시어에 컨트롤러는 서비스에서 배열로 데이터를 가져오고 그 배열을 지시문에 전달하여 막대 그래프를 만들고 싶습니다. 아래 코드의 핵심 부분을 넣었습니다. 여기 각도 - 컨트롤러 개체에 지시문 값을 바인딩하십시오.
내 최고 수준의 HTML입니다<div dash-progress
graph-data="{{dashCtrl.myProgress}}">
</div>
<div>
Other Stuff
</div>
지시어에 대한 나의 템플릿 HTML :
<div class="boxcontent" ng-show="dashCtrl.showProgress">
<div class="chart-holder-lg">
<canvas tc-chartjs-bar
chart-data="progress"
chart-options="options"
height="200"
auto-legend>
</canvas>
</div>
</div>
컨트롤러 :
angular
.module('myApp')
.controller('dashCtrl',['mySvc',
function(mySvc) {
var self = this;
this.myProgress = [];
this.getProgress = function() {
//logic must be in the service !
mySvc.getProgress().then(function(success) {
self.myProgress = mySvc.progress;
});
};
}]);
과 지침 :
angular
.module('myApp')
.directive('dashProgress', [function() {
return {
restrict: 'AE',
templateUrl: 'components/dashboard/progress.html',
scope: {
graphData: '@'
},
link: function(scope,el,attrs) {
scope.progress = {
labels: ['Duration','Percent'],
datasets: [
{
label: 'Duration',
data: [scope.graphData.duration]
},
{
label: 'Percent',
data: [scope.graphData.percent]
}
]
};
scope.options = { };
}
}
}]);
컨트롤러의 myProgress
개체의 초기 값을 설정하면 이러한 값이 지시문에 반영되지만 서비스에서 컨트롤러로 반환 될 때 필요한 실제 값을 얻지 못합니다. 대신 당신의 지침의 범위에서
단방향 바인딩이 필요하고 '='로 변경해도 아무런 차이가 없습니다. 나는 범위 설정의 각 유형을 시도했지만 어떤 차이도없는 것처럼 보인다. – jTrouble
$ scope. $ digest()를 호출하여 지시문을 다시 컴파일 할 수 있습니다. –