그래서 글로벌 메시지를 출력하는 지시문을 만들고 싶습니다. 지침의"App-Wide"메시지 시스템 작성하기
요구 ...
이 지시 메시지가 어떤 컨트롤러에서 업데이트 할 수 있습니다.
메시지가 모든 컨트롤러에서 업데이트 될 때 지시문도 결과적으로보기입니다. 뷰가
명확한 메시지 이후 지금까지 나는 함께 작업 지시문과 서비스를 만들어이 일을하고있다. 문제는 메시지가 다른 컨트롤러 내부에서 업데이트 될 때보기를 업데이트 할 수 없습니다.
누군가가 계속 진행하는 방법을 알려주면 부풀어 오를 수 있습니다. $ rootScope를 사용하고 방송하는 것은 어떨까요?
app.directive("alertMsg", ['MsgService', function(MsgService) {
return {
restrict: "E",
scope: true,
template: '{{msg}}', // this string is the html that will be placed inside the <alert-msg></alert-msg> tags.
link: function (scope, $element, attrs) {
scope.msg = MsgService.getAlertMsg(); //set msg to be available to the template above <alert-msg>{{msg}}</alert-msg>
scope.$on("$destroy", function(){ //when the <alert-msg> view is destroyed clear the alert message
MsgService.clearAlertMsg();
});
}
};
}]);
app.service('MsgService', function() {
this.alertMsg = '';
this.getAlertMsg = function(){
return this.alertMsg;
};
this.setAlertMsg = function(string) {
this.alertMsg = string;
};
this.clearAlertMsg = function(){
this.alertMsg = '';
};
});
app.controller('NewPlateController', ['urlConfig', '$scope', '$http', '$location', 'MsgService', '$routeParams', function(urlConfig, $scope, $http, $location, MsgService, $routeParams) {
$scope.plate = {license_plate: $routeParams.plate, state: 'default-state'};
// create new plate via json request
$scope.createPlate = function(){
$http.post(urlConfig.rootUrl+"/plates.js", $scope.plate).success(function(data) {
$scope.plateInfo = data;
MsgService.setAlertMsg('Plate Sucessfully Created'); //Need to update the directive to actual show this update
$location.path('/plate/'+$scope.plateInfo.plate_id);
// http error: display error messages
}).error(function(data,status,headers,config) {
$scope.errors = data;
$('#new-plate-errors').slideDown('fast');
});
};
}]);
$ rootScope와 방송의 아이디어가 좋다고 생각합니다. – Kolban
$ rootScope를 사용하는 것이 더 쉽고 깨끗하다고 생각했습니다. Im impliment하는 방법은 확실하지 않습니다. 나는 지시를 사용하지만 서비스는 사용하지 않을 것인가? – JerryA