: 여기
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
};
});
메뉴의 내용은이 지침에이 이벤트를 통신 할 수있다 업데이트 될 때 각도 이벤트를 통해 서비스를 다시 읽게됩니다. 메뉴 컨트롤러에서 $ 방송, $ 방출에 $ 청취자의
예 (https://docs.angularjs.org/api/ng/type/ $ rootScope.Scope)를 참조하십시오 :
이
$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); });
});
나는 일반적인 메뉴와 서비스를 표시하는 지침을 작성하는 것이 좋습니다 것 , 메뉴 모델을 호스팅합니다. 따라서 주 메뉴에서 모델을 변경하는 서비스를 만들 수 있으며 녹색 상자에서 새 모델을 기반으로 콘텐츠를 다시 렌더링 할 수 있습니다. –
어떻게 할 수 있습니까? –