ディレクティブの内容を更新する多数の $rootScope.$broadcast を持つコントローラーがあります。
最初の $rootScope.$broadcast はすべてのコンテンツ/データをクリアし、もう一方はデータを取り込みます。
app.controller('MyController', function($scope, header) {
$rootScope.$broadcast('clear');
$rootScope.$broadcast('title', header.title);
$rootScope.$broadcast('total', header.total);
});
注: ヘッダーは $stateProvider から解決されます。例:
.state('index', {
url: '/index',
templateUrl: 'home.html',
controller: 'MyController',
resolve: {
header: function() {
return {
title : 'Welcome to Home',
total : 6
};
}
}
})
指令:
app.directive('myDirective', function() {
return {
restrict: 'A',
replace: true,
scope: {},
link: function(scope, element, attrs) {
scope.$on("clear", function(event, val) {
scope.Title = '';
scope.Total = 0;
});
scope.$on("title", function(event, title) {
scope.Title = title;
});
scope.$on("total", function(event, total) {
scope.Total = total;
});
},
template:
'<section>' +
'<p>{{Title}} - {{Total}}</p>' +
'</section>'
}
});
私が抱えている問題は、ページの読み込み時に、クリアが完了する前にタイトルと合計の $broadcast が呼び出されているように見えることです。
アップデート:
プランカー: http://plnkr.co/edit/o7wEFyIGh0284ZAc9VnS?p=preview
home.html では常に、ディレクティブは非表示になります - 正しいです。cart.html ではいつでも、ディレクティブが表示されます - 正しいです。
問題を確認するには、カートをクリックしてください... ボタンをクリックしてカウンターを増やします..ホームに戻り、カートに戻ります.カウンターは 0 にリセットされません