:
https://plnkr.co/edit/xJoF4IuDtJlGzWmTYoqZ?p=preview
そしてここでは、角度コードの説明です:メニューの内容が更新されると
angular.module('plunker', []);
function MainCtrl($scope, menuSvc) {
// main controller sets the menu contents to the service
menuSvc.putAll([{t:"item1"},{t:"item2"}]);
}
// a very simple menu service that keeps an object of menu items
angular.module('plunker').factory("menuSvc", [ function() {
var items;
var clear = function(){
items = {};
};
var getAll = function(){
return Object.keys(items);
};
var put = function(item){
items[item.t] = item;
};
var putAll = function(itemArray, dontClean){
if(!dontClean){
clear();
}
itemArray.forEach(
function(i){
put(i);
}
);
};
clear();
return {
put: put,
getAll: getAll,
putAll: putAll,
clear: clear
};
}]);
// directive that gets its content from the service
angular.module('plunker').directive('menu', function(){
return {
restrict: 'E',
scope: {
},
templateUrl: 'menu.html',
controller: function($scope, menuSvc) {
$scope.menu = menuSvc.getAll();
},
replace: true
};
});
あなたはディレクティブにこのイベントを通信することができます。角度イベントを介して、それは彼がサービスからコテネントを再読み込みするように。私は一般的なメニューとサービスを表示するディレクティブを作成することをお勧めしたい
$scope.$on('someEvent', function(){
// update menu content
// trick is that you have to update the menu content, not to overwrite it
// for example like this:
$scope.menu.length = 0;
var newValues = menuSvc.getAll();
newValue.forEach(function(x){ $scope.menu.push(x); });
});
:$メニューコントローラでのリスナーの
例を放送し、$発し、$のために(https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope)を参照してください。メニューモデルをホストします。したがって、メインメニューからモデルを変更するサービスを作成できます。緑色のボックスでは、新しいモデルに基づいてコンテンツを再レンダリングできます。 –
私はそれを行う方法の小さな例を教えてください。 –